C ++ CMake编译错误(/ usr / bin / ld:找不到<library_name>)

时间:2016-03-22 11:00:40

标签: c++ cmake clion cereal

*我知道有很多关于这方面的问题,但是在谈论CMake时它们并没有多大帮助,因此我决定提出问题*

所以我正在研究使用CMake的CLion,以便导入并为编译器提供参数,并成功地包含(导入)位于名为“ExternalLibraries”的文件夹中的外部库(grain:将类序列化为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:我正在使用基本的os:Freya(Ubuntu 14.04)

enter image description here

3 个答案:

答案 0 :(得分:2)

我参加聚会有点晚了,但这也许会对某人有所帮助。 我在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$ 

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

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

它奏效了。

答案 1 :(得分:1)

你应该使用

link_directories(directory1 directory2 ...)

指定库目录的指令。

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

答案 2 :(得分:0)

如果添加,则可以使用

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 指令中提供的名称。

details on how to proper setup LD_LIBRARY_PATH variable discussed here