0

我无法编译使用 MagickWand 和 CMake 的程序。我收到此错误:

[ 33%] Linking CXX executable bin/ScreenRecorder
/usr/bin/ld: CMakeFiles/ScreenRecorder.dir/source/main.cpp.o: in function `std::thread::thread<void (&)(), , void>(void (&)())':
main.cpp:(.text._ZNSt6threadC2IRFvvEJEvEEOT_DpOT0_[_ZNSt6threadC5IRFvvEJEvEEOT_DpOT0_]+0x20): undefined reference to `pthread_create'
/usr/bin/ld: CMakeFiles/ScreenRecorder.dir/source/screenshot.cpp.o: in function `printScrn(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
screenshot.cpp:(.text+0xd): undefined reference to `MagickWandGenesis'
/usr/bin/ld: screenshot.cpp:(.text+0x1a): undefined reference to `NewMagickWand'
/usr/bin/ld: screenshot.cpp:(.text+0x31): undefined reference to `MagickReadImage'
/usr/bin/ld: screenshot.cpp:(.text+0x4f): undefined reference to `MagickWriteImage'
/usr/bin/ld: screenshot.cpp:(.text+0x62): undefined reference to `DestroyMagickWand'
/usr/bin/ld: screenshot.cpp:(.text+0x6b): undefined reference to `MagickWandTerminus'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/ScreenRecorder.dir/build.make:99: bin/ScreenRecorder] Error 1
make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/ScreenRecorder.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

这是我的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
project(ScreenRecorder)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall")
set(source_dir "${PROJECT_SOURCE_DIR}/source/")
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin/")

find_package(ImageMagick REQUIRED)

include_directories(
  "/usr/include/ImageMagick-6/"
)

link_libraries(
  "/usr/lib/x86_64-linux-gnu/"
)

file (GLOB src_files "${source_dir}*.cpp")

add_executable(ScreenRecorder ${src_files})

我尝试使用${ImageMagick_MagickWand_INCLUDE_DIR}${ImageMagick_MagickWand_LIBS_DIR}等变量,但它说"wand/MagickWand.h" not found。感谢您的回答。

4

1 回答 1

0

通过将自动生成的变量括在引号中,我能够解决类似的问题:

INCLUDE_DIRECTORIES(
  "${ImageMagick_INCLUDE_DIRS}"
)

TARGET_LINK_LIBRARIES(MyProject
  "${ImageMagick_LIBRARIES}"
) 

这解决了问题的原因是变量是 CMake 列表。没有引号,它们被连接成一个长字符串:

MESSAGE(${ImageMagick_INCLUDE_DIRS})
MESSAGE(${ImageMagick_LIBRARIES})

->

1> [CMake] /usr/include/ImageMagick-6/usr/include/x86_64-linux-gnu/ImageMagick-6
1> [CMake] /usr/lib/x86_64-linux-gnu/libMagickWand-6.Q16.so

但是,使用引号时,列表的不同元素使用分号分隔:

MESSAGE("${ImageMagick_INCLUDE_DIRS}")
MESSAGE("${ImageMagick_LIBRARIES}")

->

1> [CMake] /usr/include/ImageMagick-6;/usr/include/x86_64-linux-gnu/ImageMagick-6
1> [CMake] /usr/lib/x86_64-linux-gnu/libMagickWand-6.Q16.so
于 2019-09-11T20:44:04.590 回答