cmake make找不到-lprotocol

时间:2016-04-25 11:12:38

标签: cmake

我在目录中有这些文件: CMakeLists.txt datalink.c datalink.h protocol.a protocol.h

所有这些文件都来自我的老师,除了CMakeLists.txt。

#CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(DataLinkSimulator)
INCLUDE_DIRECTORIES(.)
set(SOURCE_FILES datalink.c protocol.a datalink.h protocol.h)
add_executable(DataLinkSimulator ${SOURCE_FILES})
target_link_libraries(DataLinkSimulator protocol.a)

我跑'cmake'。在这个目录中,它的工作原理。 但是,当我运行'make'时,它会给我这些错误。

Scanning dependencies of target DataLinkSimulator
[ 50%] Building C object CMakeFiles/DataLinkSimulator.dir/datalink.c.o
[100%] Linking C executable DataLinkSimulator
/usr/bin/ld: cannot find -lprotocol
collect2: 错误:ld 返回 1 #(means "error: ld return 1")
CMakeFiles/DataLinkSimulator.dir/build.make:94: recipe for target 'DataLinkSimulator' failed
make[2]: *** [DataLinkSimulator] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/DataLinkSimulator.dir/all' failed
make[1]: *** [CMakeFiles/DataLinkSimulator.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

错误“找不到-lprotocol”必须由ld的错误协议作为共享库引起。但我的协议是一个静态库。所以我很困惑。

如果我自己编辑makefile而不是使用cmake,它可以正常工作。

datalink: datalink.c datalink.h
    gcc datalink.c protocol.a -o datalink -lm -lstdc++ -m32
clean:
    ${RM} datalink
    ${RM} *.o

我使用了“make VERBOSE = 1”:

/usr/local/bin/cmake -H/home/shantom/Desktop/FileRecv/Lab1-linux -B/home/shantom/Desktop/FileRecv/Lab1-linux --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/bin/cmake -E cmake_progress_start /home/shantom/Desktop/FileRecv/Lab1-linux/CMakeFiles /home/shantom/Desktop/FileRecv/Lab1-linux/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/shantom/.wine/drive_c/Program Files/QQ/Users/1044950475/FileRecv/Lab1-linux'
make -f CMakeFiles/DataLinkSimulator.dir/build.make CMakeFiles/DataLinkSimulator.dir/depend
make[2]: Entering directory '/home/shantom/.wine/drive_c/Program Files/QQ/Users/1044950475/FileRecv/Lab1-linux'
cd /home/shantom/Desktop/FileRecv/Lab1-linux && /usr/local/bin/cmake -E cmake_depends "Unix Makefiles" /home/shantom/Desktop/FileRecv/Lab1-linux /home/shantom/Desktop/FileRecv/Lab1-linux /home/shantom/Desktop/FileRecv/Lab1-linux /home/shantom/Desktop/FileRecv/Lab1-linux /home/shantom/Desktop/FileRecv/Lab1-linux/CMakeFiles/DataLinkSimulator.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/shantom/.wine/drive_c/Program Files/QQ/Users/1044950475/FileRecv/Lab1-linux'
make -f CMakeFiles/DataLinkSimulator.dir/build.make CMakeFiles/DataLinkSimulator.dir/build
make[2]: Entering directory '/home/shantom/.wine/drive_c/Program Files/QQ/Users/1044950475/FileRecv/Lab1-linux'
[ 50%] Linking C executable DataLinkSimulator
/usr/local/bin/cmake -E cmake_link_script     CMakeFiles/DataLinkSimulator.dir/link.txt --verbose=1
/usr/bin/cc     CMakeFiles/DataLinkSimulator.dir/datalink.c.o  -o DataLinkSimulator -Wl,-Bstatic -lprotocol -Wl,-Bdynamic 
/usr/bin/ld: cannot find -lprotocol
collect2: 错误:ld 返回 1
CMakeFiles/DataLinkSimulator.dir/build.make:94: recipe for target 'DataLinkSimulator' failed
make[2]: *** [DataLinkSimulator] Error 1
make[2]: Leaving directory '/home/shantom/.wine/drive_c/Program Files/QQ/Users/1044950475/FileRecv/Lab1-linux'
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/DataLinkSimulator.dir/all' failed
make[1]: *** [CMakeFiles/DataLinkSimulator.dir/all] Error 2
make[1]: Leaving directory '/home/shantom/.wine/drive_c/Program Files/QQ/Users/1044950475/FileRecv/Lab1-linux'
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

1 个答案:

答案 0 :(得分:-1)

为了理解您需要使用verbose mode从命令行启动make命令所需的错误;在Linux paltform上它意味着启动:

make VERBOSE=1

以这种方式,您将拥有用于编译代码的整个命令行。

为了解决您的错误,您可以:

  • 添加link_directories(directory_where_you_have_library)CMake行,在target_link_libraries行之前添加您拥有protocol.a的目录
  • 使用绝对路径添加protocol.a。

你的修改后的Makefile可以工作,因为你可能在所有文件的同一个目录中都有它,而CMake从构建根目录开始在各种目录中创建了很多Makefile,所以它需要-L gcc标志(可通过link_directories获得)或-l with absolute path(可获得的添加protocol.a,绝对路径)。