0

I'm really quite a novice when it comes to the C compilation process, and I've got a question:

I have a series of files that #include header files using -Idirectory and all is well. However, I get undefined reference to '<function names>' when compiling; This is because I haven't linked to any actual code, just the headers, as far as I can tell. However, the same directory that includes the headers (-Idirectory) also includes the CPP files that have the code. However, I apparently cannot just put them on the path like: gcc file1.cpp file2.cpp directory/*.cpp -Idirectory ...., so how do I go about doing that? Google only leads me to how to include headers, which I have already done.

Name McChange
  • 2,750
  • 5
  • 27
  • 46
  • 1
    Actuallyou can do `gcc file1.cpp file2.cpp directory/*.cpp -Idirectory` although normally people don't do this because it's really slow to recompile everything if you had an error in one of the later files on the line. Normally you would use a makefile (or an IDE's project file) to specify how to build the compile command for each object file separately, and then invoke the linker at the end. – M.M Feb 25 '15 at 03:09
  • A makefile seems to be the decent way to go. – πάντα ῥεῖ Feb 25 '15 at 03:13
  • How would a makefile prevent needless recompilation? Wouldn't running `make` on it simply just execute GCC the same way manually doing it would? – Name McChange Feb 25 '15 at 03:19
  • The makefile won't re-execute `gcc` for source files that were already compiled successfully – M.M Feb 25 '15 at 03:21
  • So what would I do in this instance to not rebuild `directory` each time I compiled? Is there a documentation or guide I could read? I hate to be a nuisance but I'm by no means experienced at this at all. – Name McChange Feb 25 '15 at 03:26
  • @πάνταῥεῖ You seem to be closing all questions for duplicate claims. While I don't disagree, it doesn't help to just point beginners to some long-winded reading and tell them "here, read this". – wyas Feb 25 '15 at 04:20
  • @wyas _"You seem to be closing all questions for duplicate claims."_ Not all of them. Just if they are appearing at the same vague level, as the broadness of the dupe actually supports. – πάντα ῥεῖ Feb 25 '15 at 04:22
  • I'd argue that the linked answer doesn't answer this question; I know what a "undefined reference" is, I was asking a distinctly different question – Name McChange Feb 25 '15 at 04:57
  • @πάνταῥεῖ This particular question should be open IMO. – wyas Feb 25 '15 at 06:22

0 Answers0