Add a compiled third library to CMakelists.txt

Hi,

When I tried to add a precompiled third-party library OpenNetVM to my CMakelists.txt of a current project, I met an issue to call the functions of the third library. The third-party library has only Makefile and it generates .a files after make. Can someone tell me what something else I missed or is there anything wrong?
Here is my CMakelists.txt code to link the library.

include_directories(${ONVM_DIR}/onvm/onvm_nflib ${ONVM_DIR}/onvm/lib ${ONVM_DIR}/onvm)
target_link_libraries(${PROJECT_NAME} ${DPDK_LIBS} ${ONVM_DIR}/onvm/onvm_nflib/${RTE_TARGET}/lib/libonvm.a ${ONVM_DIR}/onvm/lib/${RTE_TARGET}/lib/libonvmhelper.a)

There is no issue when I cmake, but an issue when I build.
[ 25%] Linking CXX executable OLAS
/usr/bin/ld: CMakeFiles/OLAS.dir/main.cpp.o: in function main': main.cpp:(.text+0x18): undefined reference to onvm_nflib_stats_summary_output(unsigned short)’
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/OLAS.dir/build.make:86: OLAS] Error 1
make[1]: *** [CMakeFiles/Makefile2:96: CMakeFiles/OLAS.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

Thanks!

There are several possible cases:

  • the symbol is in a library that you did not link to
  • the library is a C library but the function is not declared extern “C”
  • you mistyped the function name and ignored the warning

Hi Hendrik,

Thanks very much for your reply!

I used an very simple CMakeLists.txt to link luajit but still have the issue to call the function. The issue is very similar to my original question.
Here are my CMakeLists.txt

ExternalProject_Add(project_luajit
  #URL http://luajit.org/download/LuaJIT-2.0.1.tar.gz
  SOURCE_DIR "/users/wcd/cmake-v3/out/build/project_luajit-prefix/src/project_luajit"
  DOWNLOAD_COMMAND ""
  #PREFIX ${CMAKE_CURRENT_BINARY_DIR}/luajit-2.0.1
  CONFIGURE_COMMAND ""
  BUILD_COMMAND make
  BUILD_IN_SOURCE true
  INSTALL_COMMAND make install
)
ExternalProject_Get_Property(project_luajit SOURCE_DIR)
message("+++++" ${SOURCE_DIR})
#message("+++++install_dir:" ${install_dir})
add_library(luajit STATIC IMPORTED)
##./out/build/project_luajit-prefix/src/project_luajit/src/libluajit.a
set_property(TARGET luajit PROPERTY IMPORTED_LOCATION ${SOURCE_DIR}/src/libluajit.a)
add_dependencies(luajit project_luajit)
add_executable(myapp main.cpp)
include_directories(${SOURCE_DIR}/src)
target_link_libraries(myapp luajit)
target_link_libraries(${PROJECT_NAME} ${SOURCE_DIR}/src/libluajit.a)

I have an very simple main.cpp which only calls the func print_usage in luajit.c but I still have the following issue.

root@node-3:/users/wcd/cmake-v3# ./build.sh 
[ 80%] Built target project_luajit
[ 90%] Building CXX object CMakeFiles/myapp.dir/main.cpp.o
/users/wcd/cmake-v3/main.cpp: In function ‘int main()’:
/users/wcd/cmake-v3/main.cpp:12:5: error: ‘print_usage’ was not declared in this scope
   12 |     print_usage();
      |     ^~~~~~~~~~~
make[2]: *** [CMakeFiles/myapp.dir/build.make:63: CMakeFiles/myapp.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:78: CMakeFiles/myapp.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

Solved this issue by the following code.

ExternalProject_Add(project_luajit
  #URL http://luajit.org/download/LuaJIT-2.0.1.tar.gz
  SOURCE_DIR "/users/wcd/cmake-v3/out/build/project_luajit-prefix/src/project_luajit"
  DOWNLOAD_COMMAND ""
  #PREFIX ${CMAKE_CURRENT_BINARY_DIR}/luajit-2.0.1
  CONFIGURE_COMMAND ""
  BUILD_COMMAND make
  BUILD_IN_SOURCE true
  INSTALL_COMMAND make install
)
ExternalProject_Get_Property(project_luajit SOURCE_DIR)
message("+++++" ${SOURCE_DIR})
add_executable(${PROJECT_NAME} main.c)
target_link_libraries(${PROJECT_NAME} ${SOURCE_DIR}/src/libluajit.so)