# Add a compiled third library to CMakelists.txt

**URL:** <https://discourse.cmake.org/t/add-a-compiled-third-library-to-cmakelists-txt/6558>\
**Category:** Code\
**Tags:** os:linux, gen:makefiles\
**Created:** [September 23, 2022, 2:56pm UTC](https://discourse.cmake.org/t/add-a-compiled-third-library-to-cmakelists-txt/6558 "2022-09-23T14:56:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Judy\_Wei](https://discourse.cmake.org/user_avatar/discourse.cmake.org/judy_wei/32/2796_2.png) [@Judy\_Wei](https://discourse.cmake.org/u/Judy_Wei)\
**Post date:** [September 23, 2022, 2:56pm UTC](https://discourse.cmake.org/t/add-a-compiled-third-library-to-cmakelists-txt/6558/1 "2022-09-23T14:56:19Z")

</div>

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.

```auto
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!

---

<div class="post-metadata">

**Author:** ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)\
**Post date:** [September 24, 2022, 7:27am UTC](https://discourse.cmake.org/t/add-a-compiled-third-library-to-cmakelists-txt/6558/2 "2022-09-24T07:27:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Judy\_Wei](https://discourse.cmake.org/user_avatar/discourse.cmake.org/judy_wei/32/2796_2.png) [@Judy\_Wei](https://discourse.cmake.org/u/Judy_Wei)\
**Post date:** [September 25, 2022, 10:39pm UTC](https://discourse.cmake.org/t/add-a-compiled-third-library-to-cmakelists-txt/6558/3 "2022-09-25T22:39:36Z")

</div>

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

```auto
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.

```auto
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

```

---

<div class="post-metadata">

**Author:** ![Judy\_Wei](https://discourse.cmake.org/user_avatar/discourse.cmake.org/judy_wei/32/2796_2.png) [@Judy\_Wei](https://discourse.cmake.org/u/Judy_Wei)\
**Post date:** [September 26, 2022, 7:28pm UTC](https://discourse.cmake.org/t/add-a-compiled-third-library-to-cmakelists-txt/6558/4 "2022-09-26T19:28:48Z")

</div>

Solved this issue by the following code.

```auto
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)

```
