Error in Linking a .so library in macOS using CMake - c++

I am trying to link a .so file that is named libtwitcurl.so.1 using CMake. My Cmake file looks like this:
cmake_minimum_required(VERSION 3.8)
project(MarkoTweeter)
set(CMAKE_CXX_STANDARD 14)
set(SOURCE_FILES main.cpp markov/markov_chain.cpp markov/markov_chain.h libraries libraries/curl)
include_directories(${CMAKE_SOURCE_DIR}/inc)
link_directories(${CMAKE_SOURCE_DIR}/libraries)
add_executable(MarkoTweeter ${SOURCE_FILES} markov/markov_chain.cpp
markov/markov_chain.h)
target_link_libraries(MarkoTweeter twitcurl)
But I keep getting this error:
[ 33%] Linking CXX executable MarkoTweeter
ld: library not found for -ltwitcurl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [MarkoTweeter] Error 1
make[2]: *** [CMakeFiles/MarkoTweeter.dir/all] Error 2
make[1]: *** [CMakeFiles/MarkoTweeter.dir/rule] Error 2
make: *** [MarkoTweeter] Error 2
For some reason it cannot find the shared library. I have tried using:
g++ main.cpp libraries/libtwitcurl.so.1
Which works fine. But I can't seem to make it work with CMake in CLion.

You need to pass absolute path to target_link_libraries.
Use find_library instead of link_directories as recommended in the official documentation:
Note that this command is rarely necessary. Library locations returned by find_package() and find_library() are absolute paths. Pass these absolute library file paths directly to the target_link_libraries() command. CMake will ensure the linker finds them.
Simple usage of find_library for your case would be:
find_library(TWIT_CURL_LIBRARY twitcurl ${CMAKE_SOURCE_DIR}/libraries)
target_link_libraries(MarkoTweeter ${TWIT_CURL_LIBRARY})

Related

CMake static library not found

im trying to link a static library (libglfw3.a). The library is place in my "lib" folder which is in the root directory of my project. I keep getting the error which can be found at the bottom. Hope someone can help me. Thanks in advance!
Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.12)
project(GLFWGLADSTUFF)
set(CMAKE_CXX_STANDARD 11)
set(SOURCES
src/main.cpp
src/glad.c
)
INCLUDE_DIRECTORIES(include)
add_executable(GLFWGLADSTUFF ${SOURCES})
target_include_directories(GLFWGLADSTUFF
PUBLIC src
PUBLIC include
)
target_link_libraries(GLFWGLADSTUFF
lib/libglfw3.a
)
Error:
Consolidate compiler generated dependencies of target GLFWGLADSTUFF
[ 33%] Linking CXX executable GLFWGLADSTUFF
ld: library not found for -llib/libglfw3.a
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [GLFWGLADSTUFF] Error 1
make[1]: *** [CMakeFiles/GLFWGLADSTUFF.dir/all] Error 2
make: *** [all] Error 2
link_directories(lib)
target_link_libraries(GLFWGLADSTUFF glfw3)
should resolve your issue. CMake is not recognizing your path as a path, but as the library name.
target_link_libraries(GLFWGLADSTUFF
lib/libglfw3.a
)
Will not work. You can:
specify the linker path and the name to be linked (i.e. other answer)
or preferably, use an IMPORTED add_library. There is even nice cmake documentation on the topic.

Linking an external library bundle file on macos with cmake

I have a library compiled as a single file that I'm trying to link with cmake but haven't been able to cobble together something that works using existing examples that should be related, eg.
CMake link an external library
Cmake linking external libraries
What I have so far:
cmake_minimum_required(VERSION 3.17)
project(atem)
set(CMAKE_CXX_STANDARD 14)
set(BMD_API_LIB "BMDSwitcherAPI")
set(BMD_API_PATH "/Library/Application Support/Blackmagic Design/Switchers/BMDSwitcherAPI.bundle/Contents/MacOS/")
find_library(BMDSwitcherAPI ${BMD_API_LIB} PATHS ${BMD_API_PATH})
add_executable(atem main.cpp BMDSwitcherAPIDispatch.cpp)
target_link_libraries(atem ${BMDSwitcherAPI})
The files main.cpp and BMDSwitcherAPIDispatch.cpp exist in the same directory. Building says that the file for the library can't be found, but the binary for the library file for ${BMD_API_LIB} cannot be found, but it is definitely at the path given in ${BMD_API_PATH}.
I'm not sure where to go from here.
Edit: added entire error message
====================[ Build | atem | Debug ]====================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/user/Code/atem/cmake-build-debug --target atem -- -j 9
[ 33%] Linking CXX executable atem
ld: library not found for -lBMDSwitcherAPI
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [atem] Error 1
make[2]: *** [CMakeFiles/atem.dir/all] Error 2
make[1]: *** [CMakeFiles/atem.dir/rule] Error 2
make: *** [atem] Error 2
This may not be helpful to anyone else because it was so specific to the one library I was using, but here it is anyway.
cmake_minimum_required(VERSION 3.17)
project(atem)
set(CMAKE_CXX_STANDARD 14)
find_library(CoreFoundation_Library CoreFoundation)
mark_as_advanced(CoreFoundation_Library)
set(
SWITCHERS_SDK_INCLUDE_DIR
/Applications/Blackmagic\ ATEM\ Switchers/Developer\ SDK/Mac\ OS\ X/include/
)
add_executable(
atem
atem/main.cpp
${SWITCHERS_SDK_INCLUDE_DIR}/BMDSwitcherAPIDispatch.cpp
)
target_include_directories(atem PRIVATE ${SWITCHERS_SDK_INCLUDE_DIR})
target_link_libraries(atem ${CoreFoundation_Library})

How to link SDL2 manually in CMake

Recently I have started to learn CMake. To practice, I am trying to link SDL2 manually. I know that there is another way around using find_file which is easy. But I want to do it myself for practice.
I get an error when I try to link the libSDL2main.a file (running the Makefile using cmd mingw32-make)
[ 50%] Linking CXX executable exe0.exe
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: cannot find -llibSDL2main
collect2.exe: error: ld returned 1 exit status
CMakeFiles\exe0.dir\build.make:105: recipe for target 'exe0.exe' failed
mingw32-make[2]: *** [exe0.exe] Error 1
CMakeFiles\Makefile2:94: recipe for target 'CMakeFiles/exe0.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/exe0.dir/all] Error 2
Makefile:102: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
Here is my CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(SDL_Test_Project)
include_directories(include)
add_executable(exe0 main.cpp)
target_link_libraries(exe0 libSDL2main.a)
Here main.cpp is only a source file. I have put SDL2.dll and libSDL2main.a into the root of the project directory. (I used the CMake GUI to generate the Makefile in Windows 10).
If you want to link to the SDL2 libraries directly in target_link_libraries() (without defining IMPORTED targets, or using find_library()), use the full path to each library. The CMAKE_SOURCE_DIR variable provides the full path to the root directory of the CMake project:
target_link_libraries(exe0 PRIVATE
mingw32
${CMAKE_SOURCE_DIR}/libSDL2main.a
${CMAKE_SOURCE_DIR}/SDL2.dll
)
Note, for SLD2, you may also have to add the mingw32 to this command when using MinGW for compilation.

How to link Google Test library to CLion project (Mac OS X El Capitan)

I'm trying to link my project with google C++ testing framework. I use Mac OS X El Capitan and I have installed test library in the default path.
lib:
/usr/local/lib/libgtest_main.a
/usr/local/lib/libgtest.a
include (for headers):
/usr/local/include/gtest
I created a new CLion (2016.1.1) project a this is the CMakeList.txt which should include the lib.
cmake_minimum_required(VERSION 3.5)
project(GoogleTest)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(GoogleTest ${SOURCE_FILES})
target_link_libraries(GoogleTest gtest gtest_main)
This is the result:
Scanning dependencies of target GoogleTest
[ 50%] Building CXX object CMakeFiles/GoogleTest.dir/main.cpp.o
[100%] Linking CXX executable GoogleTest
ld: library not found for -lgtest
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [GoogleTest] Error 1
make[1]: *** [CMakeFiles/GoogleTest.dir/all] Error 2
make: *** [all] Error 2
How can I fix this? Thanks in advance
It looks like /usr/local/lib is not in the compiler's library path list. Try specifying the full path for the libraries in target_link_libraries.
target_link_libraries(GoogleTest /usr/local/lib/libgtest.a /usr/local/lib/libgtest_main.a)

CMake "TARGET_LINK_LIBRARIES cannot find -lfoo" but it is in the same directory as another library

As mentioned above i have problems with compiling my c++ project (using CMake) which uses some dynamic libraries (.so).
There are 3 libs in my directory (i.e. home/sources/lib/). When i only tell the compiler (in the CMake file) to use the first lib (foo1.so) it works (only this file, the order does not matter). But it does not work with any of the other libs (foo2.so and foo2.so). All 3 files have the .so extension.
Note: The directory and file names were changed but the structure is the same.
The libraries i am using were not compiled / created by me and are from a 3rd party. (it would not matter when they were broken, would it?)
And this is how my CMake file looks like:
cmake_minimum_required(VERSION 3.3)
project(MyProj)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -m64")
INCLUDE_DIRECTORIES("/home/sources/include")
LINK_DIRECTORIES("/home/sources/lib")
set(SOURCE_FILES main.cpp)
add_executable(MyProj ${SOURCE_FILES})
TARGET_LINK_LIBRARIES(MyProj foo1.so)
Changing the above line to this does not work anymore:
TARGET_LINK_LIBRARIES(MyProj foo1.so foo2.so foo3.so)
Just another way to write it (does not help)
TARGET_LINK_LIBRARIES(MyProj foo1.so)
TARGET_LINK_LIBRARIES(MyProj foo2.so)
TARGET_LINK_LIBRARIES(MyProj foo3.so)
And as mentioned above: ALL 3 libraries are in the SAME directory (which i refer to with LINK_DIRECTORIES)
And this is the error i get when trying to compile with the other libs (as said only foo1.so works):
[ 50%] Linking CXX executable MyProj
/usr/bin/ld: cannot find -lfoo2
/usr/bin/ld: cannot find -lfoo3
collect2: error: ld returned 1 exit status
make[3]: *** [MyProj] Error 1
make[2]: *** [CMakeFiles/MyProj.dir/all] Error 2
make[1]: *** [CMakeFiles/MyProj.dir/rule] Error 2
make: *** [MyProj] Error 2
P.S.: I did some research before posting here but did not find any otherone with this "strange" problem. And for sure i wouldn't have come so far with my CMake file without some googling skills ^^
Not sure, but it seems to me that CMake is looking for libfoo1.so whereas the file is actually foo1.so (the same applies for foo2 and foo3)
Try "importing" the libs:
add_library(foo1 SHARED IMPORTED)
set_property(TARGET foo1 PROPERTY IMPORTED_LOCATION "/home/sources/lib/libfoo1.so")
# same thing for foo2 and foo3 ...
target_link_libraries(MyProj foo1 foo2 foo3)
EDIT
There's also the possibility to provide the full path to the library:
target_link_libraries(MyProj "/home/sources/lib/libfoo1.so"
"/home/sources/lib/libfoo2.so"
"/home/sources/lib/libfoo3.so")