1

Please bear with me if this is a naive question. I tried to see if this was answered elsewhere on SO, but I didn't quite find a question that answered this.

I see an error when I try to compile some C++ code (test.cpp). I am including some headers, as follows -

#include <iostream>
#include <string>
#include "lefrReader.hpp"

lefrReader.hpp has the definition of function lefrInit(), and is present in the folder /foo/bar/include, say.

I am compiling this code with the following (in a UNIX environment) -

g++ -I/foo/bar/include -L/foo/bar/include ./test.cpp -o ./test

However, this fails with this error -

test.cpp:(.text+0x11): undefined reference to `lefrInit()'
collect2: ld returned 1 exit status

Shouldn't having the /foo/bar/include directory in the include (-I) path help with finding lefrReader.hpp?

EDIT - I also tried the following to no avail (from What is an undefined reference/unresolved external symbol error and how do I fix it?) -

g++ -I/foo/bar/include -L/foo/bar/include ./test.cpp -o ./test -llefrReader

Error -

test.cpp:(.text+0x11): undefined reference to `lefrInit()'
collect2: ld returned 1 exit status
Barmar
  • 741,623
  • 53
  • 500
  • 612
SulfoCyaNate
  • 396
  • 1
  • 2
  • 19
  • I't not complaining that it cant find a .hpp, it's complaining about the function definition. – Jay Jan 05 '18 at 23:52
  • -I is used for compilation. Not linking. – Philip Brack Jan 05 '18 at 23:53
  • The linker doesn't care at all about header files. Functions are merely declared in header files, not defined. – William Pursell Jan 05 '18 at 23:55
  • Thanks for the feedback! I have edited the question accordingly. – SulfoCyaNate Jan 06 '18 at 00:06
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Barmar Jan 06 '18 at 00:08
  • 1
    You need to link with `lefrReader.o`. – Barmar Jan 06 '18 at 00:08
  • @Barmar, I previously went through all the scenarios in the question you linked, and did not find any that matched my question. If you think it is a duplicate, do you mind pointing to which answer to the other question helps me, please? – SulfoCyaNate Jan 06 '18 at 00:09
  • 1
    This answer [Failure to link against appropriate libraries/object files or compile implementation files](https://stackoverflow.com/a/12574400/1491895) – Barmar Jan 06 '18 at 00:10
  • @Barmar, thank you. I tried that out, and it did not work for me either. I added an edit to explain this. – SulfoCyaNate Jan 06 '18 at 00:17

1 Answers1

1

-l is for linking with libraries, so -llefRreader looks for lefrReader.a (static library) or lefrReader.so (shared library).

To link with a single object file, you just put the object file as an argument.

g++ -I/foo/bar/include ./test.cpp /foo/bar/include/lefrReader.o -o ./test

You have to previously compile that file:

g++ -I/foo/bar/include lefrReader.cpp -c -o lefrReader.o

Or you can compile and link them both at once:

g++ -I/foo/bar/include ./test.cpp /foo/bar/include/lefrReader.cpp -o ./test

However, this means you'll recompile both .cpp files even when just one of them has changed. When you start working with multiple files, it's a good time to start using a makefile, which can automate all the dependencies.

Barmar
  • 741,623
  • 53
  • 500
  • 612