I created a C++ library in visual studio and all is fine. However, I'm trying to convert the projects to CMake so I can build them on Linux as well. The library in question is implemented as pimple (I don't know if it makes any difference though) and it looks like this:
#ifndef BLINKER_H
#define BLINKER_H
/* If we are we on Windows, we want a single define for it.*/
#if !defined(_WIN32) && (defined(__WIN32__) || defined(WIN32) || defined(__MINGW32__))
#define _WIN32
#endif /* _WIN32 */
#if defined(_WIN32) && defined(_BLINKER_BUILD_DLL)
/* We are building DETECTOR as a Win32 DLL */
#define BLINKER_API __declspec(dllexport)
#elif defined(_WIN32) && defined(BLINKER_DLL)
/* We are calling DETECTOR as a Win32 DLL */
#define BLINKER_API __declspec(dllimport)
#elif defined(__GNUC__) && defined(_BLINKER_BUILD_DLL)
/* We are building DETECTOR as a shared / dynamic library */
#define BLINKER_API __attribute__((visibility("default")))
#else
/* We are building or calling DETECTOR as a static library */
#define BLINKER_API
#endif
#include <iostream>
#include <sstream>
#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
using namespace std::chrono_literals;
typedef std::chrono::system_clock Time;
const int COUNT_VJ = 3;
const int ILS_DSM = 30;
const int ILS_DSM_EXT = 600;
const cv::Size ILF_SIZE = { 34, 26 };
const cv::Size ILF_SIZE_PROCESSED = { 41, 32 };
struct Info
{
void CheckStatus()
{
//...
}
//...
};
class BlinkerImpl;
class BLINKER_API Blinker
{
public:
Blinker(std::string path = "", std::string netPath = "");
void main_op(cv::Mat& imgOrig, Info& inf, std::string sme = "IDLS_E5", bool show_debug_info = true);
~Blinker();
private:
BlinkerImpl* blinker;
};
#endif // !BLINKER_H
This is the cmake that I have written so far:
cmake_minimum_required(VERSION 3.11)
project(Blinker)
set(CMAKE_CXX_STANDARD 17)
find_package(Torch REQUIRED)
find_package(OpenCV REQUIRED)
include_directories(D:/Codes/cpp/port/LibtorchPort/Dependencies/include ${TORCH_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS})
# http://dlib.net/examples/CMakeLists.txt.html
add_subdirectory(C:/Users/User/Downloads/dlib-19.21_for_cmake/dlib-19.21 C:/Users/User/Downloads/dlib-19.21_for_cmake/dlib-19.21/build)
set(BLINKER_SRC D:/Codes/cpp/port/LibtorchPort/Dependencies/include/Blinker/Blinker.cpp
)
add_library(
Blinker_static
${BLINKER_SRC}
)
add_library(
Blinker_dynamic
SHARED
${BLINKER_SRC}
)
set(LIBS ${LIBS} ${TORCH_LIBRARIES} ${OpenCV_LIBS} dlib::dlib )
# Link
target_link_libraries(Blinker_static ${TORCH_LIBRARIES} ${OpenCV_LIBS} dlib::dlib)
target_link_libraries(Blinker_dynamic ${TORCH_LIBRARIES} ${OpenCV_LIBS} dlib::dlib)
When I run the cmake like this :
cmake -DCMAKE_PREFIX_PATH="$(python -c 'import torch.utils; print(torch.utils.cmake_prefix_path)');D:\External Libs\Opencv3.4.10_vc14_vc15\opencv3.4" ..
and then open the Visual Studio project and try to build everything, the build goes on smoothly and generates 4 files:
- Blinker_dynamic.dll
- Blinker_dynamic.exp
- Blinker_dynamic.lib
- Blinker_static.lib.
However, when I try to link a test application with the Blinker_dynamic.dll
for example, I get linker errors as if the actual Blinker implementation is no where to be found:
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "public: void __cdecl Blinker::main_op(class cv::Mat &,struct Info &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,bool)" (?main_op@Blinker@@QEAAXAEAVMat@cv@@AEAUInfo@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@_N@Z) referenced in function "int __cdecl test1(void)" (?test1@@YAHXZ) Blinker_dynamic_test D:\Codes\cpp\port\LibtorchPort\Blinker_Test\build\Blinker_Test.obj 1
Error LNK2019 unresolved external symbol "public: __cdecl Blinker::Blinker(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0Blinker@@QEAA@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z) referenced in function "int __cdecl test1(void)" (?test1@@YAHXZ) Blinker_dynamic_test D:\Codes\cpp\port\LibtorchPort\Blinker_Test\build\Blinker_Test.obj 1
Error LNK2019 unresolved external symbol "public: __cdecl Blinker::~Blinker(void)" (??1Blinker@@QEAA@XZ) referenced in function "int __cdecl test1(void)" (?test1@@YAHXZ) Blinker_dynamic_test D:\Codes\cpp\port\LibtorchPort\Blinker_Test\build\Blinker_Test.obj 1
Error LNK1120 3 unresolved externals Blinker_dynamic_test D:\Codes\cpp\port\LibtorchPort\Blinker_Test\build\Release\Blinker_dynamic_test.exe 1
And when inspecting the DLL itself, you can indeed see, there is no trace of the actual Blinker.cpp
in it.
For the reference here is the Blinker.dll
created inside Visual Studio:
and here is the DLL created using this cmake I posted above:
I must be doing something wrong, but I can't seem to figure it out! What am I missing here?
Update 1
Executing the cmake with the _BLINKER_BUILD_DLL
like this :
cmake -DCMAKE_PREFIX_PATH="$(python -c 'import torch.utils; print(torch.utils.cmake_prefix_path)');D:\External Libs\Opencv3.4.10_vc14_vc15\opencv3.4" _BLINKER_BUILD_DLL ..
results in the following error :
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.18363.
-- The C compiler identification is MSVC 19.27.29111.0
-- The CXX compiler identification is MSVC 19.27.29111.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe - broken
CMake Error at C:/Users/User/Anaconda3/Library/share/cmake-3.17/Modules/CMakeTestCCompiler.cmake:60 (message):
The C compiler
"C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: D:/Codes/cpp/port/LibtorchPort/Blinker/build/CMakeFiles/CMakeTmp
Run Build Command(s):C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/MSBuild/Current/Bin/MSBuild.exe cmTC_3b817.vcxproj /p:Configuration=Debug /p:Platform=x64 /p:VisualStudioVersion=16.0 /v:m && Microsoft (R) Build Engine version 16.7.0+b89cb5fde for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
Microsoft (R) C/C++ Optimizing Compiler Version 19.27.29111 for x64
testCCompiler.c
Copyright (C) Microsoft Corporation. All rights reserved.
cl /c /Zi /W3 /WX- /diagnostics:column /Od /Ob0 /D WIN32 /D _WINDOWS /D "CMAKE_INTDIR=\"Debug\"" /D _MBCS /Gm- /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"cmTC_3b817.dir\Debug\\" /Fd"cmTC_3b817.dir\Debug\vc142.pdb" /Gd /TC /errorReport:queue D:\Codes\cpp\port\LibtorchPort\Blinker\build\CMakeFiles\CMakeTmp\testCCompiler.c
D:\Codes\cpp\port\LibtorchPort\Blinker\build\CMakeFiles\CMakeTmp\testCCompiler.c : fatal error C1471: [D:\Codes\cpp\port\LibtorchPort\Blinker\build\CMakeFiles\CMakeTmp\cmTC_3b817.vcxproj]
D:\Codes\cpp\port\LibtorchPort\Blinker\build\CMakeFiles\CMakeTmp\testCCompiler.c : fatal error C1001: Internal compiler error. [D:\Codes\fac_ver\cpp\port\LibtorchPort\Blinker\build\CMakeFiles\CMakeTmp\cmTC_3b817.vcxproj]
(compiler file 'd:\agent\_work\7\s\src\vctools\Compiler\Utc\src\p2\main.c', line 195)
To work around this problem, try simplifying or changing the program near the locations listed above.
If possible please provide a repro here: https://developercommunity.visualstudio.com
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
CL!CloseTypeServerPDB()+0x1f640
CL!CloseTypeServerPDB()+0x1f640
CL!InvokeCompilerPass()+0x6dc83
CL!InvokeCompilerPass()+0xf0722
CL!InvokeCompilerPass()+0xf06b4
CL!CloseTypeServerPDB()+0x1f6a8
CL!CloseTypeServerPDB()+0x1f5f8
INTERNAL COMPILER ERROR in 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.27.29110\bin\HostX64\x64\CL.exe'
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:5 (project)
-- Configuring incomplete, errors occurred!
See also "D:/Codes/cpp/port/LibtorchPort/Blinker/build/CMakeFiles/CMakeOutput.log".
See also "D:/Codes/cpp/port/LibtorchPort/Blinker/build/CMakeFiles/CMakeError.log".