CMake links dynamically even though its supposed to link statically [cross compilation Linux -> Windows]

asked 2020-04-06 09:51:17 -0500

GRASBOCK gravatar image

I want to build a statically, because I need to distribute the application on multiple platforms (Linux and Windows). OpenCV has been built and installed on a linux machine statically.

Here you can see the relevant parts that are included in my CMakeLists.txt that were supposed to make OpenCV link statically

message("Compiling for windows")
set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++)
set(BUILD_SHARED_LIBS OFF) #this has no effect
set(CMAKE_LINK_LIBRARY_SUFFIX ".a") #this has no effect

set(OpenCV_STATIC ON)  #this has no effect
find_package(OpenCV REQUIRED ) 
include_directories(${OpenCV_INCLUDE_DIRS} )

During linking I get this error, as it apparently is a library required by opencv:

/usr/lib/x86_64-linux-gnu/ error adding symbols: File in wrong format
collect2: error: ld returned 1 exit status

As you can see is being used. Even though in that Folder a libpng.a file is also available.

This only happens with mingw, but using the "-static" flag inside

target_link_libraries( Tracking ${OpenCV_LIBS} "-static")

results in

/usr/bin/x86_64-w64-mingw32-ld: attempted static link of dynamic object `/usr/lib/x86_64-linux-gnu/'
collect2: error: ld returned 1 exit status

So It still uses the .so file instead of .a That is why I suspect I need to change the OpenCV configuration

I have spent hours on this, so help is appreciated.

edit retag flag offensive close merge delete