vincent
vincent

Reputation: 3

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 implementation 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?

Upvotes: 0

Views: 2491

Answers (3)

aimalbashir
aimalbashir

Reputation: 25

g++ -Wall -Werror source.cpp

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

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798626

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.

Upvotes: 2

Jacob
Jacob

Reputation: 78848

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.

Upvotes: 2

Related Questions