0

I am trying to link a chain of external libraries (liblept using libjpeg) to a project. Numerous attempts and searching the internet just caused different cmake errors which is quite confusing

The structure of the project folder:

/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

Now, the CMakeLists. The only thing I know about linking these libs is that the basic tips found all around Stack Overflow generate different errors. Except the following:

This gets the project linked, but the application crashes with 0xC0000135 (failed to find the 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)

What is wrong?

Alex
  • 1,165
  • 2
  • 9
  • 27
  • 1
    If the executable is looking for a DLL then I guess you are not statically linking the library but just the stub LIB file used by Windows to refer to DLL functions. Check this out: http://stackoverflow.com/questions/6421693/why-are-lib-files-beasts-of-such-a-duplicitous-nature. That's a problem with LIB files which could be either libraries with real implementation, either stubs used to dynamically link with a DLL. Did you try to add the DLL to the binary folder? – Jack May 15 '16 at 08:09

1 Answers1

0

Thanks to the note about .libs by Jack I found the problem. The thing is liblept168.lib is the linking file for liblept168.dll, not a static library. So, the dll should be copied to the executable folder. The resulting CMakeLists looks like this:

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>")
Alex
  • 1,165
  • 2
  • 9
  • 27