【问题标题】:CMake link a library (.a/.so)CMake 链接库 (.a/.so)
【发布时间】:2014-12-05 09:41:53
【问题描述】:

我有以下项目文件结构:

libs/
  └──FreeImage/
        ├── FreeImage.dll
        ├── FreeImage.h
        ├── FreeImage.lib
        ├── license-fi.txt
        ├── license-gplv2.txt
        └── license-gplv3.txt
main.cpp
CMakeLists.txt

我的CMakeLists.txt

cmake_minimum_required(VERSION 2.8.4)
project(untitled)

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

set(FREEIMAGE_LIBRARY_AND_HEADER_DIRECTORY libs/FreeImage)
find_path(FREEIMAGE_LIBRARY_AND_HEADER_DIRECTORY FreeImage.h)
find_library(FREEIMAGE_LIBRARY_AND_HEADER_DIRECTORY freeimage)
include_directories(${FREEIMAGE_LIBRARY_AND_HEADER_DIRECTORY})

set(SOURCE_FILES main.cpp)
add_executable(main ${SOURCE_FILES})

target_link_libraries(main freeimage)

我跟着this 写了我的CMakeLists.txt,但它对我不起作用。

输出:

/home/username/main_ssd/soft/linux/portable/clion-140.569.17/bin/cmake/bin/cmake --build /home/username/.clion10/system/cmake/generated/70336599/70336599/Debug --target main -- -j 4
-- Configuring done
-- Generating done
-- Build files have been written to: /home/username/.clion10/system/cmake/generated/70336599/70336599/Debug
Scanning dependencies of target main
[100%] Building CXX object CMakeFiles/main.dir/main.cpp.o
Linking CXX executable main
/usr/bin/ld: cannot find -lfreeimage
collect2: error: ld returned 1 exit status
CMakeFiles/main.dir/build.make:85: recipe for target 'main' failed
make[3]: *** [main] Error 1
CMakeFiles/Makefile2:60: recipe for target 'CMakeFiles/main.dir/all' failed
make[2]: *** [CMakeFiles/main.dir/all] Error 2
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/main.dir/rule' failed
make[1]: *** [CMakeFiles/main.dir/rule] Error 2
Makefile:160: recipe for target 'main' failed
make: *** [main] Error 2

【问题讨论】:

  • 附注“目录”拼写为“目录”
  • 谢谢!我已经在代码中修复了它。

标签: c++ c++11 cmake clion


【解决方案1】:

set(FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY libs/FreeImage)

变量FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY 设置为libs/FreeImage

find_path(FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY FreeImage.h)

变量FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY 设置为directory,包含命名文件FreeImage.h。由于缺少可选参数 PATHS 或 HINTS,因此不存在命令提示

find_library(FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY freeimage) 变量FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY 设置为full path 到库FreeImage(如果找到)。由于缺少可选参数 PATHS 或 HINTS,因此不存在命令提示

include_directories(${FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY})

变量FREEIMAGE_LIBRARY_AND_HEADER_DIRRECTORY 包含库FreeImage 的完整路径(如果找到)。

target_link_libraries(main freeimage)

CMakeLists.txt 中不存在名称为freeimage 的目标,因此它被解释为您要链接-lfreeimage

问题:您为什么尝试在 Linux 上使用“dll”?

【讨论】:

  • 我认为这是可能的。现在我找到了这个库的更新版本并找到了 .so 和 .a 库。
【解决方案2】:

我已经下载了新版本的 FreeImage 库(来自 here)并按照以下说明进行编译:

Compiling FreeImagePlus
-----------------------
FreeImagePlus is a C++ wrapper for FreeImage. 
To compile FreeImage as a C++ library, follow these steps : 
1) Enter the FreeImage directory
2) Build the distribution : 
make -f Makefile.fip
make -f Makefile.fip install
3) Clean all files produced during the build process
make -f Makefile.fip clean

所以我得到了以下库的文件结构:

libs/FreeImage/
├── FreeImage.h
├── FreeImagePlus.h
├── libfreeimageplus-3.16.0.so
├── libfreeimageplus.a
├── license-fi.txt
├── license-gplv2.txt
└── license-gplv3.txt

然后我像这样修改了我的 CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.4)
project(untitled)

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

find_path(SIFTGPU_INCLUDE_DIR FreeImage.h)
find_library(SIFTGPU_LIBRARY libfreeimageplus)
include_directories(${SIFTGPU_INCLUDE_DIR})

add_executable(main ${SOURCE_FILES})

target_link_libraries(main freeimageplus)

【讨论】:

    【解决方案3】:

    彼得指出你正确的方向:

    target_link_libraries(main freeimage)
    

    应替换为:

    target_link_libraries(main ${freeimage})
    

    其中${freeimage} 是包含要链接到的库的路径值的变量,使用find_library 检索。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      • 2013-02-17
      • 2020-08-12
      • 1970-01-01
      相关资源
      最近更新 更多