我尝试运行使用 cmake 生成的 makefile。它产生一个错误
ld: library not found for -lhello
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)
如果您想链接库,CMake 有一个系统。对于许多标准库,我们都有 cmake 模块,允许您使用find_package命令。这将为包含目录和库设置一些变量。如果您的库没有这样的东西,您可以使用find_path来查找包含文件,并使用find_library来搜索库。
这是你可以做的(未经测试,只是出于我的想法):
add_executable(main main.c)
target_include_directories(
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
PUBLIC ${CMAKE_SOURCE_DIR}/include/hello
)
find_library (
HELLO_LIB
NAMES hello libhello # what to look for
HINTS "${CMAKE_SOURCE_DIR}/lib" # where to look
NO_DEFAULT_PATH # do not search system default paths
)
# check if we found the library
message(STATUS "HELLO_LIB: [${HELLO_LIB}]")
if (NOT HELLO_LIB)
message(SEND_ERROR "Did not find lib hello")
endif
target_link_libraries(main
${HELLO_LIB}
)
Run Code Online (Sandbox Code Playgroud)
用于message调试您的 cmake 文件。如果您也在 cmake 中定义了库,则可以直接链接到 cmake 目标。
| 归档时间: |
|
| 查看次数: |
20006 次 |
| 最近记录: |