1

I have the following CMakeLists.txt

cmake_minimum_required(VERSION 3.12)
project( project_360_visual )
find_package( OpenCV REQUIRED )
set(SOURCE_FILES 
    ${CMAKE_SOURCE_DIR}/src/project_360_visual.cpp
    ${CMAKE_SOURCE_DIR}/src/projection.cpp)
set(INCLUDE_FILES
    ${CMAKE_SOURCE_DIR}/include/project_360_visual.h
    ${CMAKE_SOURCE_DIR}/include/projection.h)
LINK_DIRECTORIES(c:/opencv/build/bin/Release)
add_library(opencv_tracking430 SHARED IMPORTED GLOBAL)
set_target_properties(opencv_tracking430 PROPERTIES IMPORTED_LOCATION c:/opencv/build/bin/Release/opencv_tracking430.dll)
add_executable( project_360_visual ${SOURCE_FILES} ${INCLUDE_FILES})
target_include_directories(project_360_visual PUBLIC ${CMAKE_SOURCE_DIR}/include)
target_link_libraries( project_360_visual PUBLIC ${OpenCV_LIBS} opencv_tracking430)

The related Visual Studio project is generated using:

cmake ../ -G "Visual Studio 15 2017 Win64" -DCMAKE_PREFIX_PATH=c:/opencv/build

However, no matter what I do I cannot manage to link/import correctly the shared libraries in OpenCV. Could someone help me in solving this?

This is my current output from Visual Studio when I try to build:

1>------ Build started: Project: ZERO_CHECK, Configuration: Release x64 ------
1>Checking Build System
1>CMake is re-running because C:/Projects/candido/CG/build/CMakeFiles/generate.stamp is out-of-date.
1>  the file 'C:/Projects/candido/CG/CMakeLists.txt'
1>  is newer than 'C:/Projects/candido/CG/build/CMakeFiles/generate.stamp.depend'
1>  result='-1'
1>-- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.18362.
1>-- Configuring done
1>-- Generating done
1>-- Build files have been written to: C:/Projects/candido/CG/build
2>------ Build started: Project: project_360_visual, Configuration: Release x64 ------
2>LINK : fatal error LNK1181: cannot open input file 'opencv_tracking430-NOTFOUND.obj'
2>Done building project "project_360_visual.vcxproj" -- FAILED.
========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Update:

Tried the two following variations based on suggestions.

Variation 1: The following does not compile:

cmake_minimum_required(VERSION 3.12)
project( project_360_visual )
find_package( OpenCV REQUIRED )
set(SOURCE_FILES 
    ${CMAKE_SOURCE_DIR}/src/project_360_visual.cpp
    ${CMAKE_SOURCE_DIR}/src/projection.cpp)
set(INCLUDE_FILES
    ${CMAKE_SOURCE_DIR}/include/project_360_visual.h
    ${CMAKE_SOURCE_DIR}/include/projection.h)

#Linking shared libraries
add_library(opencv_tracking430 IMPORTED GLOBAL)
set_target_properties(opencv_tracking430 PROPERTIES IMPORTED_LOCATION 
    c:/opencv/build/bin/Release/opencv_tracking430.lib
)

add_executable( project_360_visual ${SOURCE_FILES} ${INCLUDE_FILES})
target_include_directories(project_360_visual PUBLIC ${CMAKE_SOURCE_DIR}/include)
target_link_libraries( project_360_visual PUBLIC ${OpenCV_LIBS} opencv_tracking430)

The error:

1>------ Build started: Project: ZERO_CHECK, Configuration: Release x64 ------
1>Checking Build System
1>CMake is re-running because C:/Projects/candido/CG/build/CMakeFiles/generate.stamp is out-of-date.
1>  the file 'C:/Projects/candido/CG/CMakeLists.txt'
1>  is newer than 'C:/Projects/candido/CG/build/CMakeFiles/generate.stamp.depend'
1>  result='-1'
1>-- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.18362.
1>CMake Error at CMakeLists.txt:13 (add_library):
1>  add_library called with IMPORTED argument but no library type.
1>
1>
1>CMake Error at CMakeLists.txt:14 (set_target_properties):
1>  set_target_properties Can not find target to add properties to:
1>  opencv_tracking430
1>
1>
1>-- Configuring incomplete, errors occurred!
1>See also "C:/Projects/candido/CG/build/CMakeFiles/CMakeOutput.log".
1>CMake Configure step failed.  Build files cannot be regenerated correctly.  Attempting to stop IDE build.
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\VC\VCTargets\Microsoft.CppCommon.targets(209,5): error MSB6006: "cmd.exe" exited with code 1.
1>Done building project "ZERO_CHECK.vcxproj" -- FAILED.
2>------ Build started: Project: project_360_visual, Configuration: Release x64 ------
2>Building Custom Rule C:/Projects/candido/CG/CMakeLists.txt
2>CMake is re-running because C:/Projects/candido/CG/build/CMakeFiles/generate.stamp is out-of-date.
2>  the file 'C:/Projects/candido/CG/CMakeLists.txt'
2>  is newer than 'C:/Projects/candido/CG/build/CMakeFiles/generate.stamp.depend'
2>  result='-1'
2>-- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.18362.
2>CMake Error at CMakeLists.txt:13 (add_library):
2>  add_library called with IMPORTED argument but no library type.
2>
2>
2>CMake Error at CMakeLists.txt:14 (set_target_properties):
2>  set_target_properties Can not find target to add properties to:
2>  opencv_tracking430
2>
2>
2>-- Configuring incomplete, errors occurred!
2>See also "C:/Projects/candido/CG/build/CMakeFiles/CMakeOutput.log".
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\VC\VCTargets\Microsoft.CppCommon.targets(209,5): error MSB6006: "cmd.exe" exited with code 1.
2>Done building project "project_360_visual.vcxproj" -- FAILED.
========== Build: 0 succeeded, 2 failed, 0 up-to-date, 0 skipped ==========

Variation 2:

This compiles, but at run-time it complains the relevant .dll cannot be found:

cmake_minimum_required(VERSION 3.12)
project( project_360_visual )
find_package( OpenCV REQUIRED )
set(SOURCE_FILES 
    ${CMAKE_SOURCE_DIR}/src/project_360_visual.cpp
    ${CMAKE_SOURCE_DIR}/src/projection.cpp)
set(INCLUDE_FILES
    ${CMAKE_SOURCE_DIR}/include/project_360_visual.h
    ${CMAKE_SOURCE_DIR}/include/projection.h)

#Linking shared libraries
add_library(opencv_tracking430 SHARED IMPORTED GLOBAL)
set_target_properties(opencv_tracking430 PROPERTIES IMPORTED_LOCATION 
    c:/opencv/build/bin/Release/opencv_tracking430.dll
)
set_target_properties(opencv_tracking430 PROPERTIES IMPORTED_IMPLIB
    c:/opencv/build/lib/Release/opencv_tracking430.lib
)

add_executable( project_360_visual ${SOURCE_FILES} ${INCLUDE_FILES})
target_include_directories(project_360_visual PUBLIC ${CMAKE_SOURCE_DIR}/include)
target_link_libraries( project_360_visual PUBLIC ${OpenCV_LIBS} opencv_tracking430)
Kevin
  • 16,549
  • 8
  • 60
  • 74
user8469759
  • 2,522
  • 6
  • 26
  • 50
  • You need to include the path to the .lib file for linking when using a DLL. – Kevin Apr 13 '20 at 17:10
  • Isn't `.lib` a static library in windows? I'm confused (I usually link against static libraries). – user8469759 Apr 13 '20 at 17:11
  • I meant you need to supply the path to the .lib, along with the .lib library file name, when using dynamic libraries (DLLs) on Windows. The .lib file itself is required for linking. – Kevin Apr 13 '20 at 17:17
  • How do I supply the path in CMake? I've just tried to add the path to `.lib`s in `LINK_DIRECTORIES`. Still it doesn't work. – user8469759 Apr 13 '20 at 17:21
  • In variant 2, the issue is solved. However, the executable needs to know where to find the DLLs. You can, for example, copy the DLLs to the same directory as the executable, or update your `Path` environment variable. – Kevin Apr 13 '20 at 18:03

1 Answers1

1

You do not link against DLL libraries. These files are for the run-time loader. Typically, DLLs will be paired with a .lib import library, which you can use for linking. The CMake syntax to import and link against a DLL with a paired .lib is as follows (use IMPORTED_IMPLIB also):

add_library(opencv_tracking430 SHARED IMPORTED GLOBAL)
set_target_properties(opencv_tracking430 PROPERTIES IMPORTED_LOCATION 
    c:/opencv/build/bin/Release/opencv_tracking430.dll
)
set_target_properties(opencv_tracking430 PROPERTIES IMPORTED_IMPLIB
    c:/opencv/build/bin/Release/opencv_tracking430.lib
)
Kevin
  • 16,549
  • 8
  • 60
  • 74
  • It doesn't work, let me update the question with the changes. – user8469759 Apr 13 '20 at 17:27
  • I think in your second proposal you might be missing either a `SHARED` or `STATIC` keyword, in your `add_library` directive I mean, I tried with `SHARED`. I can compile, but still it cannot find the .dll. – user8469759 Apr 13 '20 at 17:40
  • @user8469759 You are correct, the `add_library()` syntax appears to now required the library type. I've removed the second suggestion. – Kevin Apr 13 '20 at 18:04
  • But the first doesn't work yet. I cannot figure why it can't find the `.dll` despite I specify where it is. – user8469759 Apr 13 '20 at 18:05
  • @user8469759 If your program compiles/links successfully, then this remaining issue is a *separate* issue. This is a run-time loader problem that you can address as I've suggested in a comment above, or via CMake (see [this](https://stackoverflow.com/a/39807774/3987854) post). – Kevin Apr 13 '20 at 18:10
  • I think I did everything you said, which suggestion of yours have I missed? (reading through the other post in the mean while). – user8469759 Apr 13 '20 at 18:12
  • @user8469759 It looks like my suggestion worked, as you said, the program now compiles. My answer only addresses the link error that you encountered, which was your original question. The "DLL not found at run-time" is a separate issue, and I do not want to confuse the two. There are already many solutions for this issue posted on this site, including the ones I linked earlier. Those posts, such as [this](https://stackoverflow.com/questions/39807664/how-to-best-tell-cmake-where-to-find-dll/39807774#39807774) one, will help resolve it. – Kevin Apr 13 '20 at 18:18