Compiling multiple files in through the terminal

I have three files:

main.cpp: where the main() function is held

header.h the header file

Function_Def.cpp: where the function definitions from header.h is held.

How can I compile (with g++) through the terminal all three files?

I've tried making all of the files executable with the chmod command, but that seems to only work with one file, as when I type something like this:
g++ -o math main.cpp header.h Function_Def.cpp
, the compiler doesn't seem to recognize the header file.
Headers are literally part of the cpp file(s) in which they are #include d

g++ -o math main.cpp Function_Def.cpp

I've tried making all of the files executable with the chmod command

That just doesn't make any sense at all. C++ source files are not executable.
Last edited on
They aren't executable, but I meant in the sense of a file, so the:

chmod +x x.cpp



I followed your advise and used only the .cpp files in g++ by doing this:
g++ -o math Practise.cpp Funct_Def.cpp

and was greeted with this:


Practise.cpp:1:21: fatal error: hearing.h: No such file or directory
compilation terminated.
Funct_Def.cpp:1:21: fatal error: hearing.h: No such file or directory
compilation terminated.


Am I doing something terribly obvious?
Is the header file in your current working directory? If not, you may need to tell g++ where to look:
http://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html#Directory-Options
Yes, it is in the same directory, but I'll try out some of those.
They aren't executable, but I meant in the sense of a file, so the:

chmod +x x.cpp


That still makes no sense and makes no difference whatsoever to the compiler. Never mind.

As Hanst says, use the -I (that's a capital i) switch to tell the compiler in which directory to find the header files.
g++ -I/location/of/your/header/ -o math Practise.cpp Funct_Def.cpp
Last edited on
You've definitely called it the right name in your cpp files, yes? I notice that earlier you referred to it as header.h but your cpp files want hearing.h
Ah, well, no executables allows for less "script-esque" compiling. Thank you.

Anyway, when I type any of these in:
$ g++ -I/Programming/C++ -o math Practise.cpp Funct_Def.cpp
$ g++ -I~/Programming/C++ -o math Practise.cpp Funct_Def.cpp
$ g++ -I~/Programming/C++/hearing.h -o math Practise.cpp Funct_Def.cpp


The same error is shown.

Urgh, files.


Yeah, my file is called hearing.h. I just called it header.h earlier just for example's sake.
Last edited on
If they are in the same directory, why are you using a -I switch? And the correct form is the second.
If you have #include <hearing.h> instead of #include "hearing.h" it may make a difference
bbgst: It wasn't working without it, so I thought it was worth a try.

strongdrink:

(My sincerest apologies to everyone that had to attempt to figure out what was wrong, when it was such a stupid error. Thank you all for your help, you've shown me how great the community is when something goes wrong. You are all fantastic!)

I've been programming in C++ how many months now? I still fail on the silliest errors. I'm extremely sorry for my lack of debugging. You... You get the message, eh? Thank you, ever so much!
Last edited on
Topic archived. No new replies allowed.