0

I have a file foo.h inside which, I have a function prototype as follows

int foobar(int a);

Its definition is inside foo.cpp

int foobar(int a){
    return a + 10;
}

I have another file moo.cpp when I have the definition of demo class members, and I want to call the foobar function from inside of one of the member functions like this:

demo::random(int x){
    y = foobar(x);
}

However, it gives me a linker error undefined reference. I have tried other solutions with a scope resolution operator but it isn't working and I couldn't find any other similar question.

Edit 1: Here is the full error message.

Undefined symbols for architecture x86_64:
  "foobar(int)", referenced from:
      demo::random(int) in libdemo.a(demo.cpp.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [test_moo] Error 1
make[1]: *** [CMakeFiles/test_moo.dir/all] Error 2
make: *** [all] Error 2

Edit 2: CMake code to compile and link the code.

FILE(GLOB PK_SOURCES "src/*" "app/*")
ADD_LIBRARY(moo STATIC ${PK_SOURCES})

TARGET_INCLUDE_DIRECTORIES(moo PUBLIC ${PK_SOURCES})

FILE(GLOB SOURCES_DEMO "app/moo.cpp")
ADD_EXECUTABLE(test_demo ${SOURCES_DEMO})
TARGET_LINK_LIBRARIES(test_demo moo ${PLATFORM_LINKER_FLAGS}) 

Edit 3: My main function is inside moo.cpp. The other two files are inside src directory.

Edit 4: Added CMake tag because possibly that is the problem.

Edit 5: I changed my make file to this and it fixed the problem. Please suggest to me how should I end this thread.

FILE(GLOB PK_SOURCES "src/*" "app/*")
ADD_LIBRARY(moo STATIC ${PK_SOURCES})

TARGET_INCLUDE_DIRECTORIES(moo PUBLIC ${PK_SOURCES})

FILE(GLOB SOURCES_DEMO "app/main.cpp")
ADD_EXECUTABLE(test_moo ${SOURCES_DEMO})
TARGET_LINK_LIBRARIES(test_moo moo ${PLATFORM_LINKER_FLAGS})
molecule
  • 1,027
  • 1
  • 14
  • 27
  • 1
    This should be fine. Are you compiling both cpp files? And make sure `moo.cpp` includes `foo.h`. – cigien Apr 29 '20 at 19:02
  • @cigien yes I am.. I am using CMake and I have used `*` wildcard to compile all the cpp files in that directory – molecule Apr 29 '20 at 19:05
  • 2
    Your code should work fine as written, must be specific to how you are compiling/linking – Cory Kramer Apr 29 '20 at 19:05
  • Remember to link all your object files to the final executable. – Jesper Juhl Apr 29 '20 at 19:06
  • 1
    Can you paste the *full* error message - that may give us a clue as to where the linker *thinks* the definition should be? – Adrian Mole Apr 29 '20 at 19:06
  • @CoryKramer I just noticed that I am getting the same error even after calling it from outside the member function. – molecule Apr 29 '20 at 19:10
  • 1
    Could you please show us how you are compiling and *especially* linking your code? – Jesper Juhl Apr 29 '20 at 19:12
  • @AdrianMole I have pasted the full error message. – molecule Apr 29 '20 at 19:13
  • @molecule It's not the code that is at fault, it's how you are building your code. An undefined reference means that you aren't linking with the file containing the function definition (i.e. `foo.o`). – john Apr 29 '20 at 19:14
  • I have added the cmake code which shows how I am linking them. – molecule Apr 29 '20 at 19:18
  • @anatolyg checking. – molecule Apr 29 '20 at 19:26
  • 1
    From that CMake config it looks like you're compiling moo.cpp twice (in both libmoo and test_demo) which would be a violation of the One Definition Rule. Your CMake config doesn't actually seem to match the linker output though. A [mcve] would help this question a lot. – Miles Budnek Apr 29 '20 at 19:30
  • 1
    Also, `TARGET_INCLUDE_DIRECTORIES` call is invalid -- you feed it w/ sources instead of path(s). – zaufi Apr 29 '20 at 19:34
  • 2
    … and for the God sake: please do not use file glob for getting sources! Use explicit lists! – zaufi Apr 29 '20 at 19:34
  • @zaufi thank you for your suggestions. I am new to cmake. I will make the necessary changes. – molecule Apr 29 '20 at 19:39
  • 1
    Also, to pass the linker options there is the `target_link_options` command. – zaufi Apr 29 '20 at 19:41

0 Answers0