【发布时间】:2016-10-08 02:18:10
【问题描述】:
为了在使用 CMake 组织的项目中使用完美的最小哈希库 cmph,我在 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
非常感谢。
【问题讨论】:
-
CMPH_LIBRARIES的值是多少? -
@usr1234567 它是 /usr/share/lib,我有 .a 和 .so 文件匹配模式 libcmph*.*
-
Phism 没有在任何地方声明。你错过了
add_executable吗?它应该在您尝试将内容链接到它之前定义。您的自动 add_executable 代码看起来很奇怪,我不会使用这样的东西。这不是 CMake 风格的。 -
@usr1234567 您好,我尝试了您的建议,方法是在 Phsim/src 文件夹下的 CMakeLists.txt 中的 add_executable 定义之后移动这些命令(包括 find_package pkg_search 等)。但是还是报错“cannot specified link library for target Phsim which is not built by this project”。
-
你没有可执行的Phsim,对吧?您无法链接到该项目。请尝试一个最小的示例(一个 cpp,几行 CMake 代码)。如果这不起作用,请在此处发布。如果可行,请尝试逐步扩展它,直到您遇到困难为止。如果您无法解决该问题,请再次询问。