CMake static library not found - c++

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.

Related

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})

Build a project depending on the Swiften XMPP library with CMake

I am currently trying to setup my C++ project using CMake, I successfully included dependencies like OpenMPI, but when going to Swiften, this became complex. This is not delivered in the form a package, but as shown in the git repository linked up there. Building that wasn't an issue, but I searched quite a lot of time, without finding any way to include libraries that don't provide a .so, and no main headers.
Here's my current CMakeLists.txt, made for testing only.
cmake_minimum_required (VERSION 3.0.0)
#set the executable
project (mtest)
set(EXECUTABLE_OUTPUT_PATH .)
add_executable (mtest main.cpp)
#defines libs
find_package(MPI REQUIRED)
set(CMAKE_CXX_COMPILE_FLAGS ${CMAKE_CXX_COMPILE_FLAGS} ${MPI_COMPILE_FLAGS})
set(CMAKE_CXX_LINK_FLAGS ${CMAKE_CXX_LINK_FLAGS} ${MPI_LINK_FLAGS})
include_directories(SYSTEM ${MPI_INCLUDE_PATH})
include_directories(SYSTEM Swiften)
target_link_libraries(mtest ${MPI_LIBRARIES})
add_library(swiften SHARED IMPORTED)
set_target_properties(swiften PROPERTIES
IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/Swiften/libSwiften.a"
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/Swiften"
)
target_link_libraries(mtest swiften)
And when trying to run make, here's the error :
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/Documents/CTests/build
[ 50%] Building CXX object CMakeFiles/mtest.dir/main.cpp.o
/home/user/Documents/CTests/main.cpp:12:10: fatal error: Swiften/Client/Client.h: No such file or directory
#include <Swiften/Client/Client.h>
^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
CMakeFiles/mtest.dir/build.make:62: recipe for target 'CMakeFiles/mtest.dir/main.cpp.o' failed
make[2]: *** [CMakeFiles/mtest.dir/main.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/mtest.dir/all' failed
make[1]: *** [CMakeFiles/mtest.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
By the way, if the directory structure may help you, here's a tree output : https://pastebin.com/giPb9229

Using CLion, only "main.cpp" being built

I've been working on a project in CLion for quite some time now (over a month). All of a sudden, CLion will not build all of my files... When I run my program, my messages build looks like this:
Scanning dependencies of target main.cpp
[ 11%] Building CXX object CMakeFiles/main.cpp.dir/Custom_Types.cpp.o
[ 22%] Linking CXX executable ../bin/main.cpp
ld: library not found for -l*.cpp
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
make[3]: *** [../bin/main.cpp] Error 1
make[2]: *** [CMakeFiles/main.cpp.dir/all] Error 2
make[1]: *** [CMakeFiles/main.cpp.dir/rule] Error 2
make: *** [main.cpp] Error 2
Great... what the flippin fireworks is going on? This is what my CMake looks like...
cmake_minimum_required(VERSION 3.7)
project(Crazy_Sniper)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -fpermissive")
find_package (OpenGL REQUIRED)
find_package (GLUT REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR} ${GLUT_INCLUDE_DIRS})
file(GLOB SOURCE_FILES
*.cpp
*.h
)
add_executable(main.cpp ${SOURCE_FILES} Custom_Types.cpp)
target_link_libraries (main.cpp ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES}
mdl pthread)
Does anyone know what the problem is? I've looked through countless forums, and I cannot seem to find someone with a similar problem. Any help would be much appreciated.
Update! I've managed to get CLion to build my files, but main still does nothing... I try simply printing something out, and the console does nothing. I get an error code 11 for some reason...?
Thank you!

Error in Linking a .so library in macOS using CMake

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})

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)