fatal error LNK1107 for CMake's UNKNOWN IMPORTED target - cmake

In my CMake project, I'm copying external libraries that I hold in a special folder to my output folder and import them as an IMPORTED target.
For example, for libusb:
find_path(LibUsb_INCLUDE_DIR NAMES libusb.h PATHS ${CMAKE_CURRENT_SOURCE_DIR}/LibUsb/Include)
if(NOT LibUsb_INCLUDE_DIR)
message(FATAL_ERROR "LibUsb: include directory wasn't found")
endif()
find_library(LibUsb_LIBRARY NAMES usb-1.0 libusb-1.0 PATHS ${OUTPUT_BIN_DIR})
if(NOT LibUsb_LIBRARY)
message(FATAL_ERROR "LibUsb: library wasn't found")
endif()
add_library(LibUsb UNKNOWN IMPORTED GLOBAL)
set_target_properties(LibUsb PROPERTIES IMPORTED_LOCATION ${LibUsb_LIBRARY})
set_target_properties(LibUsb PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${LibUsb_INCLUDE_DIR})
message(STATUS "LibUsb: found at ${LibUsb_LIBRARY}")
The CMake output is:
...
-- Building for: Visual Studio 15 2017
-- The C compiler identification is MSVC 19.0.24234.1
-- The CXX compiler identification is MSVC 19.0.24234.1
...
-- CMAKE SYSTEM: Windows-10.0.17763
-- CMAKE VERSION: 3.13.4
...
-- LibUsb: copy binaries
-- VsCan: copy binaries
-- LibUsb: found at C:/myproj/Build/Windows-Debug/Output/Bin/libusb-1.0.lib
-- VsCan: found at C:/myproj/Build/Windows-Debug/Output/Bin/vs_can_api.lib
...
LibUsb is found correctly and if I print LibUsb_LIBRARY the path is correct. Everything compiles correctly on Linux.
On Windows compilation I'm getting fatal error LNK1107: invalid or corrupt file.
What am I doing wrong?

Related

Why does CMake not include Fortran module files of a third party Fortran library?

I am trying to compile a single Fortran source file main.f90 with cmake that includes a single external Fortran library:
USE RAD_Module
The project compiles fine with the following simple, handwritten makefile:
all:
gfortran -c -I/usr/local/rad_v2.0.0/include main.f90
gfortran -L/usr/local/rad_v2.0.0/lib -lRAD -o test.out main.o
The external static library does not have a CMakeLists.txt and its structure looks like this:
Its *.mod module files are located in /usr/local/rad_v2.0.0/include/.
Its librad.a file is located in /usr/local/rad_v2.0.0/lib/.
My CMakeLists.txt file looks like this:
cmake_minimum_required(VERSION 3.16 FATAL_ERROR)
project("myProject" Fortran)
enable_language(Fortran)
## Target from sources:
add_executable(myProject main.f90)
## Link library:
link_directories("/usr/local/rad_v2.0.0/lib")
link_directories("/usr/local/rad_v2.0.0/include")
find_library(RAD
NAMES librad.a
HINTS "/usr/local/rad_v2.0.0/lib")
if(NOT RAD)
message(FATAL_ERROR "RAD library not found!")
endif()
target_link_libraries(myProject RAD)
With this CMakeLists.txt cmake successfully produces a makefile. However, when I subsequently try to make the code, I get the following error message:
15 | USE RAD_Module
| 1
Fatal Error: Cannot open module file 'rad_module.mod' for reading at (1): No such file or directory
compilation terminated.
The file rad_module.mod is located in the folder /usr/local/rad_v2.0.0/include.
The comment by Pierre de Buyl pointed me to the solution. In order to include the *.mod files, I had to add the line:
target_include_directories(myProject PUBLIC "/usr/local/rad_v2.0.0/include")
after calling add_executable().
Furthermore I had to link the path ${RAD} itself, instead of the object RAD in the following call:
target_link_libraries(myProject ${RAD})
This successfully links the library.
Lastly, linking the library directories with link_directories() had no impact and I removed the corresponding lines entirely.
The working CMakeLists.txt looks like this:
cmake_minimum_required(VERSION 3.16 FATAL_ERROR)
project("myProject" Fortran)
enable_language(Fortran)
## Target from sources:
add_executable(myProject main.f90)
## Link library:
find_library(RAD
NAMES librad.a
HINTS "/usr/local/rad_v2.0.0/lib")
if(NOT RAD)
message(FATAL_ERROR "RAD library not found!")
endif()
target_include_directories(myProject PUBLIC "/usr/local/rad_v2.0.0/include")
target_link_libraries(myProject ${RAD})

Crazy error when running a connector c++ program

I have a problem with Connector/C++.
I'm using CLion as IDE and want to create a c++ program to interact with mysql database.
this is my CMakeList.txt file which i include c++/connector static and dynamic libraries in it:
cmake_minimum_required(VERSION 3.15)
project(cpp_programming)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(MYSQL_CPPCONN_DIR "C:/Program Files/MySQL/MySQL Connector C++ 8.0")
include_directories(${PROJECT_NAME} PUBLIC ${MYSQL_CPPCONN_DIR}/include)
add_executable(${PROJECT_NAME} main.cpp)
# Static Libraries
target_link_libraries(${PROJECT_NAME} ${MYSQL_CPPCONN_DIR}/lib64/vs14/libcrypto.lib)
target_link_libraries(${PROJECT_NAME} ${MYSQL_CPPCONN_DIR}/lib64/vs14/libssl.lib)
target_link_libraries(${PROJECT_NAME} ${MYSQL_CPPCONN_DIR}/lib64/vs14/mysqlcppconn.lib)
target_link_libraries(${PROJECT_NAME} ${MYSQL_CPPCONN_DIR}/lib64/vs14/mysqlcppconn8.lib)
target_link_libraries(${PROJECT_NAME} ${MYSQL_CPPCONN_DIR}/lib64/vs14/mysqlcppconn8-static.lib)
target_link_libraries(${PROJECT_NAME} ${MYSQL_CPPCONN_DIR}/lib64/vs14/mysqlcppconn-static.lib)
# Dynamic Link Libraries
target_link_libraries(${PROJECT_NAME} ${PROJECT_SOURCE_DIR}/libcrypto-1_1-x64.dll)
target_link_libraries(${PROJECT_NAME} ${PROJECT_SOURCE_DIR}/libssl-1_1-x64.dll)
target_link_libraries(${PROJECT_NAME} ${PROJECT_SOURCE_DIR}/mysqlcppconn-7-vs14.dll)
target_link_libraries(${PROJECT_NAME} ${PROJECT_SOURCE_DIR}/mysqlcppconn8-2-vs14.dll)
And i just include xdevapi.h header in my c++ source file like this:
#include <iostream>
#include <mysqlx/xdevapi.h>
using namespace std;
int main()
{
return 0;
}
And i run file in Release mode in clion and i receive these errors:
Error message i see in the clion console
====================[ Build | cpp_programming | Release ]=======================
"C:\Program Files\JetBrains\CLion 2019.3.2\bin\cmake\win\bin\cmake.exe" --build C:\Users\Kianoush\CLionProjects\cpp_programming\cmake-build-release --target cpp_programming -- -j 2
Scanning dependencies of target cpp_programming
[ 50%] Building CXX object CMakeFiles/cpp_programming.dir/main.cpp.obj
[100%] Linking CXX executable cpp_programming.exe
CMakeFiles\cpp_programming.dir/objects.a(main.cpp.obj):main.cpp:(.text$_ZNK6mysqlx4abi22r05Value5printERSo[_ZNK6mysqlx4abi22r05Value5printERSo]+0x21): undefined reference to `mysqlx::abi2::r0::DbDoc::print(std::ostream&) const'
CMakeFiles\cpp_programming.dir/objects.a(main.cpp.obj):main.cpp:(.text$_ZNK6mysqlx4abi22r05Value5printERSo[_ZNK6mysqlx4abi22r05Value5printERSo]+0x2c): undefined reference to `mysqlx::abi2::r0::common::Value::print(std::ostream&) const'
CMakeFiles\cpp_programming.dir/objects.a(main.cpp.obj):main.cpp:(.text$_ZNK6mysqlx4abi22r08internal14Warning_detail5printERSo[_ZNK6mysqlx4abi22r08internal14Warning_detail5printERSo]+0x87): undefined reference to `mysqlx::abi2::r0::string::Impl::to_utf8[abi:cxx11](mysqlx::abi2::r0::string const&)'
CMakeFiles\cpp_programming.dir/objects.a(main.cpp.obj):main.cpp:(.rdata$_ZTCN6mysqlx4abi22r05ValueE0_NS1_6common5ValueE[_ZTCN6mysqlx4abi22r05ValueE0_NS1_6common5ValueE]+0x20): undefined reference to `mysqlx::abi2::r0::common::Value::print(std::ostream&) const'
CMakeFiles\cpp_programming.dir/objects.a(main.cpp.obj):main.cpp:(.rdata$.refptr._ZTVN6mysqlx4abi22r05DbDocE[.refptr._ZTVN6mysqlx4abi22r05DbDocE]+0x0): undefined reference to `vtable for mysqlx::abi2::r0::DbDoc'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\cpp_programming.dir\build.make:97: recipe for target 'cpp_programming.exe' failed
CMakeFiles\Makefile2:74: recipe for target 'CMakeFiles/cpp_programming.dir/all' failed
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/cpp_programming.dir/rule' failed
mingw32-make.exe[3]: *** [cpp_programming.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/cpp_programming.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/cpp_programming.dir/rule] Error 2
mingw32-make.exe: *** [cpp_programming] Error 2
Makefile:117: recipe for target 'cpp_programming' failed
Do i make mistakes in linking dll files or static files ?
What solution do you suggest ?
please help me, this take me in trouble for many days.
Full screen image
There are a couple of issues here.
You do not need to link all of these different libraries. You really only should require one mysqlcppconn library to be linked. To locate the library, try using find_library(), and use it for linking instead. Your CMake file should reduce to something like this:
cmake_minimum_required(VERSION 3.15)
project(cpp_programming)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(MYSQL_CPPCONN_DIR "C:/Program Files/MySQL/MySQL Connector C++ 8.0")
include_directories(${PROJECT_NAME} PUBLIC ${MYSQL_CPPCONN_DIR}/include)
add_executable(${PROJECT_NAME} main.cpp)
# Find the mysqlcppconn library.
find_library(mysqlcppconn_LIB
mysqlcppconn8
HINTS ${MYSQL_CPPCONN_DIR}/lib/vs14
# Static Libraries
target_link_libraries(${PROJECT_NAME} PUBLIC ${mysqlcppconn_LIB})
I'm not sure if you are using the ssl and crypto libraries, so add those back in if needed.
Your libraries (VisualC++) do not match the compiler (MinGW) you are using. To my knowledge, the MySQL Connector C++ downloads do not provide a MinGW set of libraries; they only provide libraries that are built with the Visual Studio compiler. Thus, you need to switch to use the VisualC++ compiler to use these libraries. Another option would be to download the MySQL source and try to build it with MinGW, but that may be more difficult.
Hope this helps!

Static linking of OpenSSL Crypto in CMake

I need to make a cross-compiled OpenSSL for a MIPS device. I've tried following the documentation. Set OPENSSL_USE_STATIC_LIBS to true and set target_link_libraries to the library files you need.
CMakeLists.txt:
compileAsC99()
if(NOT ${use_http})
message(FATAL_ERROR "program being generated without HTTP support")
endif()
set(program_c_files
...
)
set(program_h_files
...
)
include_directories(...)
add_executable(program ${program_c_files} ${program_h_files})
set(OPENSSL_USE_STATIC_LIBS TRUE)
#target_link_libraries(program OpenSSL::Crypto)
target_link_libraries(program /home/program/mips/lib/libssl.so.1.1)
target_link_libraries(program /home/program/mips/lib/libcrypto.so.1.1)
It compiles fine without warnings, but checking the resulting binary tells me that it's still shared library.
readelf -d program:
Dynamic section at offset 0x1bc contains 35 entries:
Tag Type Name/Value
0x00000001 (NEEDED) Shared library: [libssl.so.1.1]
0x00000001 (NEEDED) Shared library: [libcrypto.so.1.1]
0x0000000f (RPATH) Library rpath: [/home/program/mips/lib]
I don't understand what I'm doing wrong.
EDIT: Already looked at Linking statically OpenSSL crypto library in CMake but it didn't tell me anything new.
EDIT 2: Updated the CMakeLists.txt file according to the reply:
CMakeLists.txt:
compileAsC99()
if(NOT ${use_http})
message(FATAL_ERROR "program being generated without HTTP support")
endif()
set(program_c_files
...
)
set(program_h_files
...
)
include_directories(...)
add_executable(program ${program_c_files} ${program_h_files})
find_package(OpenSSL REQUIRED)
if(OPENSSL_FOUND)
set(OPENSSL_USE_STATIC_LIBS TRUE)
message("OPENSSL FOUND!")
endif()
target_link_libraries(program OpenSSL::Crypto)
Output:
-- IoT Client SDK Version = 1.2.11
-- Provisioning client OFF
-- target architecture: GENERIC
-- Cross compiling not using pkg-config
-- Found CURL: /home/program/mips/lib/libcurl.a (found version "7.63.0")
-- Found CURL: /home/program/mips/lib/libcurl.a
-- target architecture: GENERIC
-- target architecture: GENERIC
-- target architecture: GENERIC
-- target architecture: GENERIC
-- iothub architecture: GENERIC
OPENSSL FOUND!
-- Configuring done
-- Generating done
EDIT PROSPERITY:
If you, future people, run into the undefined reference to dlopen, I added the following to my CMakeLists.txt file
target_link_libraries(program ${CMAKE_DL_LIBS})
Setting to TRUE, variable OPENSSL_USE_STATIC_LIBS forces find_package(OpenSSL) to search the static library. So this variable works only with that call, and if you use its results:
set(OPENSSL_USE_STATIC_LIBS TRUE)
find_package(OpenSSL REQUIRED)
target_link_libraries(program OpenSSL::Crypto)
If you have already executed cmake without setting of OPENSSL_USE_STATIC_LIBS, then you need to remove CMake cache (CMakeCache.txt under build directory) before new attempt. Otherwise, already found (shared!) libraries will be used and no re-search will be performed.

Linking to libuv. Can't use vsbuild.bat version, own compilation links correctly

After sucessfully building libuv on windows in a mingw64 environment, I'm having a problems linking the libuv.dll/libuv.lib that are built as part of the vsbuild.bat build script.
When I build libuv by myself from the sources to produce either a .dll or .lib I can link without any issues.
The errors I get back when including the static or shared lib from the vsbuild.bat are related to undefined references e.g.:
[ 9%] Linking CXX shared library ..\buildcmake\bin\libuws.dll
CMakeFiles\uws.dir/objects.a(Group.cpp.obj): In function `uS::Async::start(void (*)(uS::Async*))':
D:/test/uWebSockets/git/src/Libuv.h:37: undefined reference to `uv_async_init'
CMakeFiles\uws.dir/objects.a(Group.cpp.obj): In function `uS::Async::close()':
D:/test/uWebSockets/git/src/Libuv.h:45: undefined reference to `uv_close'
...etc
The specific part of my cmake script that deals with the linking is:
find_library(LIBUV_STATIC_LIBRARY NAMES libuv.lib PATHS ${LIBUV_DEPS_DIR}/git/Debug ${LIBUV_DEPS_DIR}/git/Release PATH_SUFFIXES ${CMAKE_BUILD_TYPE}/lib NO_DEFAULT_PATH)
find_library(LIBUV_SHARED_LIBRARY NAMES libuv.dll PATHS ${LIBUV_DEPS_DIR}/git PATH_SUFFIXES .libs ${CMAKE_BUILD_TYPE} NO_DEFAULT_PATH)
MESSAGE(STATUS ".${LIBUV_STATIC_LIBRARY}")
MESSAGE(STATUS ".${LIBUV_SHARED_LIBRARY}")
include_directories(${UWS_DIR} ${LIBUVDIR_SRC} ${ZLIB_DIR_INCLUDE} ${LIBUVDIR_INCLUDE} ${OPENSSL_INCLUDE_DIR} )
add_library(uws SHARED ${UWS_SOURCES})
target_link_libraries(uws PUBLIC ${LIBUV_SHARED_LIBRARY} ${OPENSSL_SSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY})
add_definitions(-D_WIN32_WINNT=0x0A00)
cmake output shows the libraries are found ok...
-- .D:/test/deps/libuv/git/Debug/lib/libuv.lib
-- .D:/test/deps/libuv/git/Debug/libuv.dll
Version of CMake is 3.9.something and I'm using nu-mingw with g++.

What are the function calls like `BCI2000_INCLUDE( "SOURCEFILTER" )` in CMake?

I want to compile the Emotiv EPOC module for BCI2000. In that module CMakeLists file contains following(below the following error log).
The compiler gives the following error,
-- Selecting Windows SDK version to target Windows 10.0.10586.
CMake Error at CMakeLists.txt:21 (BCI2000_INCLUDE):
Unknown CMake command "BCI2000_INCLUDE".
CMake Warning (dev) in CMakeLists.txt:
No cmake_minimum_required command is present. A line of code such as
cmake_minimum_required(VERSION 3.10)```
CMakeLists.txt contains this,
###########################################################################
## $Id$
## Authors: griffin.milsap#gmail.com
## Description: Build information for the Emotiv module
# Set the executable name
SET( EXECUTABLE_NAME Emotiv )
# Set the project specific sources
SET( SRC_PROJECT
EmotivADC.cpp
lib/edk.imports.cpp
)
SET( HDR_PROJECT
EmotivADC.h
lib/edk.imports.h
)
INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/lib/include )
BCI2000_INCLUDE( "SOURCEFILTER" )
IF( WIN32 )
# Create the signal source module
BCI2000_ADD_SIGNAL_SOURCE_MODULE(
"${EXECUTABLE_NAME}"
"${SRC_PROJECT}" "${HDR_PROJECT}"
"${CMAKE_CURRENT_SOURCE_DIR}/dll/edk.dll"
"${CMAKE_CURRENT_SOURCE_DIR}/dll/edk_utils.dll"
)
ELSE()
MESSAGE( "--- No Emotiv libraries for this OS. Will not build." )
ENDIF()
How can I fix this?
Looks like they are function calls, but how do I import those function calls from another file if they are declared somewhere?
The project has to be compiled according to the instructions in this,
https://www.bci2000.org/mediawiki/index.php/Programming_Reference:Build_System
And it says the supported Visual Studio editions are 9(2008) and 10 only or you can compile it with CodeBlocks with MinGW.
And the supportive modules should be included into the source folder of BCI2000 and compile it as a whole project not as seperate modules. Then it recognized the functions defined in the parent project.
There are cMakeList files for submodules. But they cannot be compiled on their own.