如何使用CMake在我的项目中链接已编译的cmph库

时间:2016-10-08 02:18:11

标签: c++ cmake

为了使用cmph,一个完美的最小哈希库,在我使用CMake组织的项目中,我在ubuntu机器上安装了cmph库,并使用名为main.c的单个c文件对其进行了测试。

如果我尝试使用gcc 5.3.0使用以下命令编译此文件:

gcc main.c

我将获得以下输出

/tmp/ccSOH5ob.o: In function `main':
testperfect.c:(.text+0x63): undefined reference to   `cmph_io_vector_adapter'
testperfect.c:(.text+0x73): undefined reference to `cmph_config_new'
testperfect.c:(.text+0x88): undefined reference to `cmph_config_set_algo'
testperfect.c:(.text+0x9b): undefined reference to `cmph_config_set_mphf_fd'
testperfect.c:(.text+0xa7): undefined reference to `cmph_new'
testperfect.c:(.text+0xb7): undefined reference to `cmph_config_destroy'
testperfect.c:(.text+0xca): undefined reference to `cmph_dump'
testperfect.c:(.text+0xd6): undefined reference to `cmph_destroy'
testperfect.c:(.text+0xe2): undefined reference to `cmph_load'
testperfect.c:(.text+0x118): undefined reference to `cmph_search'
testperfect.c:(.text+0x153): undefined reference to `cmph_destroy'
testperfect.c:(.text+0x15f): undefined reference to  `cmph_io_vector_adapter_destroy'

但是如果我运行这个命令:

gcc main.c $(pkg-config --libs cmph) -o main

它将被编译并正常运行。

现在我需要在项目中添加一段类似的代码,CMakeList.txt就是这样编写的:

set(PROJECT_EXECUTABLE ${PROJECT_NAME}) 

# Compiling flags. 
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR 
   CMAKE_CXX_COMPILER_ID MATCHES "Clang") 
  set(CMAKE_CXX_FLAGS_RELEASE        "-O2") 
  set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g") 
  set(CMAKE_CXX_FLAGS_DEBUG          "-g") 
  set(CMAKE_CXX_FLAGS_MINSIZEREL     "-Os") 
  endif() 
# Inject project config. 
configure_file( 
  ${PROJECT_INCLUDE_DIR}/phsim/config.hpp.in 
  ${PROJECT_INCLUDE_DIR}/phsim/config.hpp 
  ) 

include(FindPkgConfig) 
find_package(PkgConfig REQUIRED) 
pkg_search_module(CMPH REQUIRED cmph) 
target_link_libraries(Phsim ${CMPH_LIBRARIES}) 
target_include_directories(Phsim PUBLIC ${CMPH_INCLUDE_DIRS}) 
target_compile_options(Phsim PUBLIC ${CMPH_CFLAGS_OTHER}) 

# Compile executable. 
file(GLOB SOURCES ${PROJECT_SRC_DIR}/*.cpp) 
add_executable(${PROJECT_EXECUTABLE} ${SOURCES}) 

set_target_properties(${PROJECT_EXECUTABLE} PROPERTIES 
  VERSION ${PHSIM_VERSION_LITER} 
  ) 

然后我尝试运行cmake。和make,但只收到错误消息:

CMake Error at src/CMakeLists.txt:20 (target_link_libraries):
  Cannot specify link libraries for target "Phsim" which is not built by this
  project.

但除非我编译项目,否则我不会获得目标可执行文件。如果我尝试在没有与库链接相关的命令的情况下编译我的项目,编译器将在我的问题开头提供类似的链接错误。

我检查了以下问题:

Undefined reference to cmph functions even after installing cpmh library

我尝试了这些网站提供的说明:

https://cmake.org/Wiki/CMake:How_To_Find_Libraries

https://cmake.org/cmake/help/v3.6/module/FindPkgConfig.html

非常感谢提前。

1 个答案:

答案 0 :(得分:0)

终于解决了。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label for="translatorNeeded">
Translator needed
<input type="radio" value="1" name="translatorNeeded">
Yes
<input type="radio" value="0" checked="checked" name="translatorNeeded">
No -> Div below should be hidden!
</label>
<br>
<br>
<label for="translator">
<label class="lcapp-label clearfix" for="translator">
Translator Name + Language
<input type="text" name="name" size="5">
<input type="text" name="lang" size="5">
</label>
</label>

确保首先包含pkgconfig并在调用&#34; add_executable&#34;

后添加链接操作

@ usr1234567感谢您的关注。