4

My Problem

I read somewhere that to add include paths to g++ you need to use the -I option:

g++ -I /some/directory and_then_my_files.cpp

However, I'm not sure if this is actually what I need. It's currently not working for me, and I couldn't find anything else close to what I have.

My Setup

I've got a directory with all my current project code, and in it a subdirectory classes, that contains various .h and .cpp files containing classes that may or may not be used when compiling my main files.

Since g++ ./classes/*.cpp main.cpp takes a long time (large number of files in classes directory), I'm looking for an alternative that only compiles and links the files that are included in the main file.

Main file:

#include "classes/a.h"
#include "classes/b.h"
// ... my code

And as you can imagine, g++ complains about undefined references to classes A and B, unless I add ./classes/*.cpp to the build command.

What I want to achieve

So -I and -L did not work, and adding the whole directory to the build command results in a ridiculously long build time - I'm talking 3-5 minutes, which really slows down my development speed.

Is there any way to only build/link the included classes from my classes directory, such as only classes A and B from the example I gave above?

Small Recap of what I've already tried

g++ -I ./classes main.cpp -o main

g++ -L ./classes main.cpp -o main (probably stupid but I tried it anyway)

g++ ./classes/*.cpp main.cpp -o main (what I currently have to resort to)


Thanks in advance :)

Mecha
  • 244
  • 3
  • 11
  • 1
    if you use `#include "classes/a.h"` then you need to add the directory to `-I` that contains the `classes` directory, not `classes` itself. Also, if you just want to compile and not link you need to add the `-c` flag or you need to add the `*.o` that you already compiled. Better yet, just use a build-system or a makefile – PeterT Nov 09 '14 at 13:54
  • If you use a Makefile, when you change something, it will only recompile what changed. A few old compilers used to work like you seem to want (Oracle Solaris Studio still has an option to do it), but with most compilers you will need to be explicit about what needs to be compiled. – Marc Glisse Nov 09 '14 at 13:56
  • I suggest using CMake. It will make your life easier. – John Zwinck Nov 09 '14 at 13:57
  • I think I might have incorrectly explained the problem. It's not a problem with including the files, that's working fine. It's more about linking. The files are included no problem, but the linker `ld` fails to find the references to the classes included in those class files. – Mecha Nov 09 '14 at 14:05
  • @Mekku well, like I tried to tell you, you need to run `g++ -c classes/someclass.cpp -o classes/someclass.o` on all the classes and then `g++ -c main.cpp -o main.o` and then finally `g++ main.o classes/someclass.o classes/moreclasses.o -o main` to do linking and compiling seperately. As you see, doing this manually is tedious, so you should use a build-system. Using a build-system also prevents you from manually making sure you didn't forget to compile one file that you modified the source to, or compiling a file for which you already compiled the most recent version. – PeterT Nov 09 '14 at 14:13
  • possible duplicate of [How do I include a path to libraries in g++](http://stackoverflow.com/questions/6141147/how-do-i-include-a-path-to-libraries-in-g) – Christophe Nov 09 '14 at 14:19
  • I see. I've always been stubborn enough to do everything manually. Guess I'll have a look at what build systems are available. Thanks, and sorry for the n00bness :P – Mecha Nov 09 '14 at 15:31

1 Answers1

0

A temporary workaround

export CPLUS_INCLUDE_PATH=/usr/local/include
nikoo28
  • 2,961
  • 1
  • 29
  • 39