3

*我知道对此有很多问题,但在谈论 CMake 时它们根本没有多大帮助,因此我决定提出这个问题 *

因此,我正在研究 CLion,它使用 CMake 导入并为编译器提供参数,并成功包含(导入)了位于名为“ExternalLibraries”的文件夹中的外部库(谷物:将类序列化为 json 文件)我的项目文件夹的根目录。它工作得很好,直到我重新启动 IDE 并尝试再次运行代码......它返回了一个编译错误(我认为)。

我的 CMake 文件如下所示:

cmake_minimum_required(VERSION 3.3)
project(xMemory)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

include_directories ("${PROJECT_SOURCE_DIR}/ExternalLibraries/cereal-1.1.2/include/")

set(SOURCE_FILES main.cpp xObject.cpp xObject.h)
add_executable(xMemory ${SOURCE_FILES})
target_link_libraries (xMemory cereal)

当我尝试运行/编译时,shell 给了我这个:

/home/lunaticsoul/Documents/clion-1.2.4/bin/cmake/bin/cmake --build /home/lunaticsoul/.CLion12/system/cmake/generated/95701c38/95701c38/Debug0 --target xMemory -- -j 4
Scanning dependencies of target xMemory
[ 33%] Building CXX object CMakeFiles/xMemory.dir/xObject.cpp.o
[ 66%] Building CXX object CMakeFiles/xMemory.dir/main.cpp.o
[100%] Linking CXX executable xMemory
/usr/bin/ld: cannot find -lcereal
collect2: error: ld returned 1 exit status
make[3]: *** [xMemory] Error 1
make[2]: *** [CMakeFiles/xMemory.dir/all] Error 2
make[1]: *** [CMakeFiles/xMemory.dir/rule] Error 2
make: *** [xMemory] Error 2

我不确定发生了什么,因为库似乎实际上已导入代码(包括谷物时没有红色字母),正如我之前所说,我认为它刚刚停止工作。

有人可以告诉我我的 CMake 文件是否有问题吗?

PD:这是一张截图,以防万一有人需要。

PD2:我正在使用基本操作系统:Freya(Ubuntu 14.04)

在此处输入图像描述

4

3 回答 3

4

你应该使用

link_directories(directory1 directory2 ...)

指令来指定库目录。

https://cmake.org/cmake/help/v3.0/command/link_directories.html

于 2016-03-22T11:51:51.973 回答
3

我参加聚会有点晚了,但也许这会对某人有所帮助。我与googletest有一个非常相似的问题:

dpoliaru@host:~/Documents/test/gtest/build$ make
Scanning dependencies of target s1
[ 50%] Building CXX object CMakeFiles/s1.dir/s1.cpp.o
[100%] Linking CXX executable s1
/usr/bin/ld: cannot find -lgtest
/usr/bin/ld: cannot find -lgmock
/usr/bin/ld: cannot find -lgtest
/usr/bin/ld: cannot find -lgmock
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/s1.dir/build.make:84: s1] Error 1
make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/s1.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
dpoliaru@host:~/Documents/test/gtest/build$

这是我用来调试它的一种方法:

确保您可以手动构建:

编译:

g++ -c -m64 -I/home/dpoliaru/.local/lib/pkgconfig/../../include s1.cpp                                                                                 

和链接:

g++ -L/home/dpoliaru/.local/lib/pkgconfig/../../lib s1.o -o s1 -lgtest -lgmock -lpthread

确保 cmake 在生成阶段正确定位库:

dpoliaru@host:~/Documents/test/gtest/build$ cmake -L .. -D CMAKE_BUILD_TYPE=Debug
-- The C compiler identification is GNU 9.2.1
-- The CXX compiler identification is GNU 9.2.1
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1") 
-- Checking for module 'gtest'
--   Found gtest, version 1.10.0
-- Checking for module 'gmock'
--   Found gmock, version 1.10.0
-- Configuring done
-- Generating done
-- Build files have been written to: /home/dpoliaru/Documents/test/gtest/build
-- Cache values
CMAKE_BUILD_TYPE:STRING=Debug
CMAKE_INSTALL_PREFIX:PATH=/usr/local
pkgcfg_lib_GMOCK_gmock:FILEPATH=/home/dpoliaru/.local/lib/libgmock.a
pkgcfg_lib_GMOCK_gtest:FILEPATH=/home/dpoliaru/.local/lib/libgtest.a
pkgcfg_lib_GMOCK_pthread:FILEPATH=/usr/lib/x86_64-linux-gnu/libpthread.so
pkgcfg_lib_GTEST_gtest:FILEPATH=/home/dpoliaru/.local/lib/libgtest.a
pkgcfg_lib_GTEST_pthread:FILEPATH=/usr/lib/x86_64-linux-gnu/libpthread.so
dpoliaru@host:~/Documents/test/gtest/build$ 

最后, rin ,我使用cmake-commands(7)根据@mustafagonlu 建议CMakeLists.txt更新了链接器目录搜索路径。target_link_directories()

target_link_directories(${PROJECT_NAME} PUBLIC ${GTEST_LIBRARY_DIRS} ${GMOCK_LDFLAGS})

它奏效了。

于 2020-02-19T09:01:44.520 回答
1

如果添加:

target_link_libraries (xMemory /library_build_path/libcereal.a)

细节:

ld 正在寻找一个非常短的文件夹列表中定义的库

/etc/ld.so.conf

它通常如下所示:

include /etc/ld.so.conf.d/*.conf

这些 *.conf 文件中的实际路径列表通常如下所示:

# Legacy biarch compatibility support
/lib32
/usr/lib32
# Multiarch support
/usr/local/lib/x86_64-linux-gnu
/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu

如果您的项目链接库不在此列表的文件夹中,则 ld 不会找到它,除非特殊链接变量设置LD_LIBRARY_PATH与您的库的路径或 cmake target_link_libraries指令中提供的完整路径/库名称。

有关如何正确设置此处讨论的 LD_LIBRARY_PATH 变量的详细信息

于 2020-10-17T18:31:23.457 回答