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})