开发者

Compiling using Command Line g++ Question

I'm trying to create a Makefile which compiles a specific program.

I have 1x .cpp file and 2x .h files.

So I would go

g++ source.cpp header1.h header2.h -o programOut

The thing is i'm getting an error.

Mainly in the second header file where Appointments is a class defined in the first header file. The second header file mainly contains (function prototypes?) I just got the other functions that I used, removed the implementati开发者_JAVA百科on and placed it in there.

error: ‘Appointments’ does not name a type
error: ISO C++ forbids declaration of ‘left’ with no type
error: ‘Appointments’ does not name a type
error: ISO C++ forbids declaration of ‘right’ with no type
error: ‘string’ does not name a type
error: ‘time_t’ does not name a type

It compiles fine if I go g++ source.cpp -o programOut however when I added the .h files when i type g++, it gives me the error shown above. Any ideas why?


Typically, you should not include header files in your list of files to compile. Rather, your source files (like source.cpp) will already #include the headers they need. If you actually have non-prototype code in your headers that you need compiled into their own .o files, you're doing it wrong.


g++ expects the files passed on the command line to be compilation units. Header files are not compilation units, and as such should not be passed on the command line in the first place.


g++ -Wall -Werror source.cpp

but you have to be in the same directory where the code is present.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜