我正在尝试将一系列外部库(使用libjpeg的liblept)链接到项目。许多尝试和搜索互联网只会导致不同的cmake错误,这是非常令人困惑的
项目文件夹的结构:
/Project
|leptonica
|--|include
| |--|leptonica
| | |--|<allheaders.h used by main.cpp is here>
| | |<jpeglib.h used by Leptonica is here>
| |lib
| |--|<.lib files here>
|CMakeLists.txt
|main.cpp
现在,CMakeLists。关于链接这些库我唯一知道的是,Stack Overflow周围的基本技巧会产生不同的错误。除以下情况外:
这会使项目链接,但应用程序崩溃0xC0000135
(找不到dll)。
cmake_minimum_required(VERSION 3.5)
project(Project)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_library(liblept168 STATIC IMPORTED)
set_target_properties(
liblept168
PROPERTIES LINKER_LANGUAGE CXX
IMPORTED_LOCATION %Project%/leptonica/lib/liblept168.lib)
add_library(libjpeg8c-static-mtdll STATIC IMPORTED)
set_target_properties(
libjpeg8c-static-mtdll
PROPERTIES LINKER_LANGUAGE CXX
IMPORTED_LOCATION %Project%/leptonica/lib/libjpeg8c-static-mtdll.lib)
link_directories(leptonica/lib)
include_directories(leptonica/include)
include_directories(leptonica/include/leptonica)
add_executable(Project "${SOURCE_FILES}")
target_link_libraries(Project liblept168 libjpeg8c-static-mtdll)
有什么问题?
答案 0 :(得分:0)
感谢杰克关于.lib
的说明我发现了问题。问题是liblept168.lib
是liblept168.dll
的链接文件,而不是静态库。因此,应将dll复制到可执行文件夹中。生成的CMakeLists
如下所示:
cmake_minimum_required(VERSION 3.5)
project(Jpeg_to_values)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_library(liblept168 SHARED IMPORTED)
set_target_properties(
liblept168
PROPERTIES LINKER_LANGUAGE CXX
IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/leptonica/lib/liblept168.dll
IMPORTED_IMPLIB ${PROJECT_SOURCE_DIR}/leptonica/lib/liblept168.lib)
add_library(libjpeg8c-static-mtdll STATIC IMPORTED)
set_target_properties(
libjpeg8c-static-mtdll
PROPERTIES LINKER_LANGUAGE CXX
IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/leptonica/lib/libjpeg8c-static-mtdll.lib)
link_directories(leptonica/lib)
include_directories(leptonica/include)
include_directories(leptonica/include/leptonica)
add_executable(Jpeg_to_values "${SOURCE_FILES}")
target_link_libraries(Jpeg_to_values liblept168 libjpeg8c-static-mtdll)
add_custom_command(
TARGET Jpeg_to_values POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${PROJECT_SOURCE_DIR}/leptonica/lib/liblept168.dll"
"$<TARGET_FILE_DIR:Jpeg_to_values>")