I try to use cmake with an external library but i can't. I use minGW and g++ compiler.
My project is organised as follow:
project/
CMakeLists.txt
src
main.cpp
lib
win
x86
SDL2-2.0.3 (contains SDL2.dll, SDL2.lib, SDL2main.lib and SDL2test.lib
include
SDL2 (contains all header)
bin
The cmake generator works `cmake -G"Eclipse CDT4 - Unix Makefiles"
My cmake file:
`
#
# File generated by CMakeBuilder
#
#
cmake_minimum_required(VERSION 2.6)
PROJECT ( project CXX )
################ INCLUDE Libs ####################
SET (CMAKE_INCLUDE_PATH "${PROJECT_SOURCE_DIR}/include/")
SET (CMAKE_LINKER_PATH "${PROJECT_SOURCE_DIR}/lib/win/x86/SDL2-2.0.3/")
message(STATUS "INCLUDE_PATH: ${CMAKE_INCLUDE_PATH}" )
message(STATUS "LINKER_PATH: ${CMAKE_LINKER_PATH}" )
INCLUDE_DIRECTORIES(${CMAKE_INCLUDE_PATH})
LINK_DIRECTORIES(${CMAKE_LINKER_PATH})
set (CMAKE_CXX_FLAGS "-lSDL2main -lSDL2 -lmingw32 ")
#Generate file source
file( GLOB_RECURSE source_files src/*)
ADD_EXECUTABLE(project ${source_files})
set(EXECUTABLE_OUTPUT_PATH bin/${CMAKE_BUILD_TYPE})
But when i launch make, i obtain this error:
`
CMakeFiles/project.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x0): multiple
definition of main
C:/Users/Gege/workspaces/workspaceCPP/project/lib/win/x86/SDL2-2.0.3/SDL2m
ain.lib(./Release/SDL_windows_main.obj):(.text[_main]+0x0): first defined here
Warning: corrupt .drectve at end of def file
C:/Users/Gege/workspaces/workspaceCPP/project/lib/win/x86/SDL2-2.0.3/SDL2m
ain.lib(./Release/SDL_windows_main.obj):(.text[_main]+0x12): undefined reference
to SDL_main'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:/Users/Gege/workspaces/workspaceCPP/project/lib/win/x86/SDL2- 2.0.3/SDL2main.lib(./
Release/SDL_windows_main.obj): bad reloc address 0x8 in section .text[_WinMain#
16]'
collect2.exe: error: ld returned 1 exit status
CMakeFiles/project.dir/build.make:87: recipe for target 'bin/project.exe'
failed
make[2]: *** [bin/project.exe] Error 1
CMakeFiles/Makefile2:60: recipe for target 'CMakeFiles/project.dir/all' faile
d
make[1]: *** [CMakeFiles/arduinoGeo.dir/all] Error 2
Makefile:75: recipe for target 'all' failed
make: *** [all] Error 2
`
Actually i don't want to put my lib in minGW folder to keep the build easy for the others.
Thanks for you help
Related
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.
I've been trying to get SDL2 to link with my project but I think my cmake file is no good. I get this error
====================[ Build | Chip_8_Interpreter | Debug ]======================
"C:\Program Files\JetBrains\CLion 2018.3.4\bin\cmake\win\bin\cmake.exe" --build "D:\Dropbox\Programming\Chip-8 Interpreter\cmake-build-debug" --target Chip_8_Interpreter -- -j 4
[ 50%] Linking CXX executable Chip_8_Interpreter.exe
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [Chip_8_Interpreter.exe] Error 1
CMakeFiles\Chip_8_Interpreter.dir\build.make:87: recipe for target 'Chip_8_Interpreter.exe' failed
mingw32-make.exe[2]: *** [CMakeFiles/Chip_8_Interpreter.dir/all] Error 2
CMakeFiles\Makefile2:71: recipe for target 'CMakeFiles/Chip_8_Interpreter.dir/all' failed
mingw32-make.exe[1]: *** [CMakeFiles/Chip_8_Interpreter.dir/rule] Error 2
CMakeFiles\Makefile2:83: recipe for target 'CMakeFiles/Chip_8_Interpreter.dir/rule' failed
mingw32-make.exe: *** [Chip_8_Interpreter] Error 2
Makefile:117: recipe for target 'Chip_8_Interpreter' failed
And currently my CMakeLists.txt looks like this
cmake_minimum_required(VERSION 3.14)
project(Chip_8_Interpreter)
set(CMAKE_CXX_STANDARD 14)
# include cmake/FindSDL2.cmake
set(SDL2_PATH "C:\\SDL2-2.0.12\\i686-w64-mingw32")
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
add_executable(Chip_8_Interpreter main.cpp)
target_link_libraries(Chip_8_Interpreter ${SDL2_LIBRARY} -lmingw32 -lSDL2main -lSDL2 -mwindows)
And my main.cpp is just the basic SDL example. I've also recently installed MinGW and SDL2, so they should both be the newest stable release.
I have been trying to write a code in C++ under Windows, that uses a third party library (I have .dll files, .lib files and .h files). I tried so many configurations in CMAKE (I am using CLion), but none of them worked! still cannot find the library. So I decided to use the plain g++ command. Still nothing!! The image below shows file tree
This is my latest cmakelists.txt:
cmake_minimum_required(VERSION 3.12)
project(tf_cert_mngr)
set(CMAKE_CXX_STANDARD 11)
set(LIBRARIES_DIR ${PROJECT_SOURCE_DIR}/openssl)
include_directories(src)
link_directories(${LIBRARIES_DIR}/lib)
include_directories(${LIBRARIES_DIR}/include)
#include_directories(include openssl\\include openssl\\lib2)
set(SOURCE_FILES src/Certificate.cpp src/tf-cert-mngr.cpp)
set(HEADER_FILES include/Certificate.h include/tf-cert-mngr.h)
find_package(ssl )
find_package(crypto)
add_executable(tf_cert_mngr ${SOURCE_FILES} ${HEADER_FILES})
target_link_libraries(tf_cert_mngr libssl.lib libcrypto.lib)
its result:
[ 33%] Linking CXX executable tf_cert_mngr.exe
c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot find -lssl
c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot
find -lcrypto
collect2.exe: error: ld returned 1 exit status
CMakeFiles\tf_cert_mngr.dir\build.make:101: recipe for target 'tf_cert_mngr.exe' failed
mingw32-make.exe[3]: *** [tf_cert_mngr.exe] Error 1
CMakeFiles\Makefile2:71: recipe for target 'CMakeFiles/tf_cert_mngr.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/tf_cert_mngr.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/tf_cert_mngr.dir/rule] Error 2
CMakeFiles\Makefile2:83: recipe for target 'CMakeFiles/tf_cert_mngr.dir/rule'
failed
Makefile:117: recipe for target 'tf_cert_mngr' failed
mingw32-make.exe: *** [tf_cert_mngr] Error 2
and this is the latest g++ command I used:
g++ src\tf-cert-mngr.cpp src\Certificate.cpp -Iinclude -I openssl\include -L openssl\lib -llibcrypto.lib -l libssl.lib -o tester
its result:
c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot find -llibcrypto.lib
c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot find -llibssl.lib
collect2.exe: error: ld returned 1 exit status
Have a look at the documentation for FindOpenSSL.cmake. This is the script that is called internally when you call:
find_package(OpenSSL) (note that for portability, case is important).
Notice that the script creates two targets:
This module defines the following IMPORTED targets:
OpenSSL::SSL
The OpenSSL ssl library, if found.
OpenSSL::Crypto
The OpenSSL crypto library, if found.
So try changing your CMakeLists.txt file to read:
find_package(OpenSSL REQUIRED)
...
target_link_libraries(tf_cert_mngr OpenSSL::SSL OpenSSL::Crypto)
Reference: https://cmake.org/cmake/help/latest/module/FindOpenSSL.html
I have a project structure like this which I wish to build using CMake.
root\
|
|--crystal\
| |
| |--include\ Math.h, Window.h
| |--src\ Math.cpp, Window.cpp
| |--lib\
| |--CMakeLists.txt // the CHILD cmake
|
|--game\ main.cpp
|--CMakeLists.txt // the PARENT cmake
The crystal sub-project is supposed to produce a static library (libcrystal.a) in the lib/ folder (using the contents of include/ and src/) and the root project will produce an executable out of game/main.cpp linking the libcrystal.a static library.
The Parent CMAKE is as follows:
cmake_minimum_required(VERSION 2.8.1)
project(thegame)
set(CRYSTAL_LIB_DIR lib)
set(CRYSTAL_LIB_NAME crystal)
add_subdirectory(${CRYSTAL_LIB_NAME})
set(LINK_DIR ${CRYSTAL_LIB_NAME}/${CRYSTAL_LIB_DIR})
set(SRCS game/main.cpp)
link_directories(${LINK_DIR})
include_directories(${CRYSTAL_LIB_NAME}/include)
add_executable(thegame ${SRCS})
target_link_libraries(thegame lib${CRYSTAL_LIB_NAME}.a)
The child CMAKE is as follows:
cmake_minimum_required(VERSION 2.8.1)
project(crystal)
include_directories( include )
file(GLOB_RECURSE SRC "src/*.cpp")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CRYSTAL_LIB_DIR})
add_library(${CRYSTAL_LIB_NAME} STATIC ${SRC})
What isn't working:
When I executed cmake . and sudo make in the root/ directory I expected both parent and child cmake to run sequentially. But it seems like the child cmake is not getting invoked and thus not producing the .a file. Its showing some error like:
Scanning dependencies of target thegame
[ 20%] Building CXX object CMakeFiles/thegame.dir/game/main.cpp.o
[ 40%] Linking CXX executable thegame
/usr/bin/ld: cannot find -lcrystal
collect2: error: ld returned 1 exit status
CMakeFiles/thegame.dir/build.make:94: recipe for target 'thegame' failed
make[2]: *** [thegame] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/thegame.dir/all' failed
make[1]: *** [CMakeFiles/thegame.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
What is working:
I went ahead the did this
executed cmake . in root/
navigate into the crystal folder and manually invoked the Makefile by sudo make
came to the root/ again and invoked the outer Makefile by sudo make
and this perfectly worked.
Question:
Why the child CMake is not getting invoked as I mentioned in the What isn't working section ???
Use target_link_libraries(thegame ${CRYSTAL_LIB_NAME}) in the root CMakeLists.txt and remove link_directories call. CMake will recognize that you are linking to crystal target and set makefile dependencies and compiler flags accordingly.
I'm trying to link a downloaded library called ArduinoJson to my project with CMake, but it seems that whenever i include the file it is missing, it will just ask for the next file its missing. I will end up with a lot of include lines which i think isnt the right way. I cant seem to find the correct syntax to include a libary in CMake
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.11)
SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
SET(CMAKE_CXX_FLAGS "-DTESTING")
include_directories(
/usr/share/arduino/libraries/ArduinoJson/include
/usr/share/arduino/hardware/arduino/cores/arduino
)
set(files
DataHandler.h
DataHandler.cpp
JSON.cpp
JSON.h
HAL_mock.cpp
HAL_mock.h
HAL_Protocol.h
lib/ArduinoJson/ArduinoJson.h
)
# Locate GTest
find_package(GTest REQUIRED)
# Link runTests with what we want to test and the GTest library
add_executable(runTests DataHandlerTests.cpp JSONTests.cpp HALTests.cpp ${files})
target_link_libraries(runTests GTest::Main ArduinoJson)
The error I get
[ 14%] Building CXX object CMakeFiles/runTests.dir/JSONTests.cpp.o
In file included from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/../Internals/../String.hpp:14:0,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/../Internals/DynamicStringBuilder.hpp:11,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/../Internals/JsonPrintable.hpp:12,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/../JsonVariant.hpp:13,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/../JsonBuffer.hpp:14,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/BlockJsonBuffer.hpp:10,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/DynamicJsonBuffer.hpp:10,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson.hpp:10,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson.h:8,
from /usr/share/arduino/libraries/ArduinoJson/ArduinoJson.h:8,
from /home/zizyx/Desktop/git-sensorNetwerk/sensor-network/gtest/JSON.h:6,
from /home/zizyx/Desktop/git-sensorNetwerk/sensor-network/gtest/JSONTests.cpp:5:
/usr/share/arduino/hardware/arduino/cores/arduino/WString.h:29:26: fatal error: avr/pgmspace.h: No such file or directory
compilation terminated.
CMakeFiles/runTests.dir/build.make:86: recipe for target 'CMakeFiles/runTests.dir/JSONTests.cpp.o' failed
make[2]: *** [CMakeFiles/runTests.dir/JSONTests.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/runTests.dir/all' failed
make[1]: *** [CMakeFiles/runTests.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I would like to add 2 maybe 3 lines which include all the libraries i need for my ArduinoJson library