英文:
How properly use find_library?
问题
我有这个文件结构:
我试图创建可执行文件,但在创建makefile时遇到了问题。我像这样使用CMake创建了libsort:
cmake_minimum_required(VERSION 3.22)
project(sort)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/deploy)
set(SOURCE_FILES src/sort.cpp)
set(HEADERS_FILES include/sort.h)
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${HEADERS_FILES})
target_include_directories(${PROJECT_NAME} PUBLIC include)
通过这种方式,我在deploy文件夹中创建了libsort.so。现在我想创建可执行文件,所以我尝试从主文件夹运行CMake,并得到以下结果:
-- C编译器标识是GNU 11.3.0
-- CXX编译器标识是GNU 11.3.0
-- 检测C编译器ABI信息
-- 检测C编译器ABI信息 - 完成
-- 检查工作中的C编译器:/usr/bin/cc - 跳过
-- 检测C编译器功能
-- 检测C编译器功能 - 完成
-- 检测CXX编译器ABI信息
-- 检测CXX编译器ABI信息 - 完成
-- 检查工作中的CXX编译器:/usr/bin/c++ - 跳过
-- 检测CXX编译器功能
-- 检测CXX编译器功能 - 完成
CMake错误在CMakeLists.txt:5处(find_library):
无法使用以下名称找到LIB_FILE:sort
在app文件夹中的CMakeLists:
cmake_minimum_required(VERSION 3.22)
project(my_app)
find_library(LIB_FILE sort HINTS ${CMAKE_BINARY_DIR}/lib/build/deploy REQUIRED)
add_executable(my_app main.cpp)
target_link_libraries(${PROJECT_NAME} ${LIB_FILE})
我尝试过HINTS,也尝试了不使用HINTS以及许多其他find_library()的可能性。有人知道如何正确做吗?
英文:
And i am trying to create executable file, but i have problem in road to this, when i am creating makefile.
I created libsort by cmake like this:
cmake_minimum_required(VERSION 3.22)
project(sort)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/deploy)
set(SOURCE_FILES src/sort.cpp)
set(HEADERS_FILES include/sort.h)
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${HEADERS_FILES})
target_include_directories(${PROJECT_NAME} PUBLIC include)
And by this i created libsort.so in deploy folder.
Now i want to create executable file, so i am trying to run cmake from main folder and i am getting in return:
-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:5 (find_library):
Could not find LIB_FILE using the following names: sort
CMakeLists in folder app:
cmake_minimum_required(VERSION 3.22)
project(my_app)
find_library(LIB_FILE sort HINTS ${CMAKE_BINARY_DIR}/lib/build/deploy REQUIRED)
add_executable(my_app main.cpp)
target_link_libraries(${PROJECT_NAME} ${LIB_FILE})
I tried with HINTS, without and many possibilities with find_library().
Anyone have idea how to do this properly?
答案1
得分: 2
find_library()
的使用在这里是不正确的,它用于查找库,而不是源代码。请使用 add_subdirectory()
:
cmake_minimum_required(VERSION 3.22)
project(my_app)
add_subdirectory(lib sort)
add_executable(my_app main.cpp)
target_link_libraries(${PROJECT_NAME} sort)
英文:
find_library()
usage is incorrect here, it is used for looking for libraries, but not for sources. Use add_subdirectory()
:
cmake_minimum_required(VERSION 3.22)
project(my_app)
add_subdirectory(lib sort)
add_executable(my_app main.cpp)
target_link_libraries(${PROJECT_NAME} sort)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论