1

I have a main.cpp file in a directory called test that has an

#include "INIReader.h"

The structure of the files is like below

---/test/main.cpp

---/inih/ini.h
         ini.c

---/inih/cpp/INIReader.h
             INIReader.cpp

INIReader.cpp has these includes

#include "../ini.h"
#include "INIReader.h"

I am trying to compile main.cpp in one line with

g++ -o test -Wall -I../inih/cpp main.cpp

but it is not working. Errors like

> Undefined symbols for architecture x86_64: 
>... REFERENCES TO stuff in INIReader ...
>ld: symbol(s) not found
> for architecture x86_64 clang: error: linker command failed with exit
> code 1 (use -v to see invocation)

show up.

Right now I am compiling by running the commands and it will work.

> g++ -c -Wall ../inih/cpp/INIReader.cpp 
> g++ -c -Wall ../inih/ini.c 
> g++ -c -Wall -I../inih/cpp main.cpp 
> g++ -o configtest main.o INIReader.o ini.o

I thought that the g++ compiler will look for each #include in each of the default include directories and the passed -I directory and do not know why the above one liner does not work. Will g++ "follow" includes in all files like the ./ini.h? I have not been able to find a good resource for learning this. How can I combine these command into one line like the previous command? Thanks.

Edit:

I have looked at the "duplicate" question but not understand how it relates to my problem. I am trying to find a good resource for learning how to "include" correctly with g++ and know why my current one liner does not work. It is not a problem in the code, as compiling and then linking one by one is successful!

Duplicate question was similar, but did not cover including files in other directories.

ansonl
  • 786
  • 1
  • 13
  • 34
  • 1
    The error you are getting is coming from the linker step, not the compiler. It doesn't have anything to do with your includes. You have to tell the linker all of the object files that are required to make the target binary. – forsvarir May 27 '15 at 18:52
  • So is there a one liner command for g++ to accomplish this? – ansonl May 27 '15 at 23:47
  • possible duplicate of [Using G++ to compile multiple .cpp and .h files](http://stackoverflow.com/questions/3202136/using-g-to-compile-multiple-cpp-and-h-files) – forsvarir May 28 '15 at 08:27

1 Answers1

2

The error you are getting is coming from the linker step, not the compiler step. It doesn't have anything to do with your includes. You need to tell the linker all the files required to make the target binary. As has been said in this answer, which may be the question this was previously closed as a duplicate of, you just need to list all of the cpp files after main in the command line. So for yours, the command line should be something like:

g++ -o configtest -Wall -I../inih/cpp main.cpp ../inih/cpp/INIReader.cpp ../inih/ini.c

Once you start compiling all of the files in one step, you may have to add an extra include instruction to find header files in different paths, so your command line might need to be:

g++ -o configtest -Wall -I../inih/cpp -I../inih main.cpp ../inih/cpp/INIReader.cpp ../inih/ini.c

But that depends how you're including your headers from your source files...

Community
  • 1
  • 1
forsvarir
  • 10,749
  • 6
  • 46
  • 77