I have build/CMakeLists.txt file:
cmake_minimum_required (VERSION 2.8)
project(Tetris)
include_directories(
"headers"
)
set(SFML_DEPENDENCIES)
set(SFML_DEPENDENCIES ${SFML_SYSTEM_DEPENDENCIES} ${SFML_DEPENDENCIES})
set(SFML_DEPENDENCIES ${SFML_GRAPHICS_DEPENDENCIES} ${SFML_DEPENDENCIES})
set(SFML_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} ${SFML_DEPENDENCIES})
set(CMAKE_CXX_COMPILER /usr/bin/g++-5)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -g")
set(EXECUTABLE_NAME "tetris")
file(GLOB SOURCES *.cpp)
add_executable(${EXECUTABLE_NAME} ${SOURCES})
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
When I run cmake .. && make I have a screen of messages saying I did not include SFML library:
undefined reference to `sf::VertexArray::operator[](unsigned long)'
undefined reference to `sf::Keyboard::isKeyPressed(sf::Keyboard::Key)'
and so on. I also tried to include flags to CMAKE_CXX_FLAGS variable:
-lsfml-graphics -lsfml-window -lsfml-system it doesn't work either. But if I run g++ from the console directly all compiles just fine:
g++-5 --std=c++11 -Wall main.cpp Game.cpp Window.cpp Board.cpp -lsfml-graphics -lsfml-window -lsfml-system
I use Linux Mint if it has any difference.
You need to find the location of SFML package, and add include directories and link.
In your CMakeLists.txt:
Change this line:
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
By:
# Detect and add SFML
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
#Find any version 2.X of SFML
#See the FindSFML.cmake file for additional details and instructions
find_package(SFML 2 REQUIRED system window graphics)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
endif()
Source
EDITED:
For SFML v1, change find_package line by:
find_package(SFML 1 REQUIRED system window graphics)
Related
I am trying to make a CMAKE file that allows me to cross compile a WxWidgets application From Ubuntu For Windows.
My tools are all installed correctly, and I am able to run:
i686-w64-mingw32.static-gcc main.cpp $(i686-w64-mingw32.static-wx-config --cxxflags --libs) -o main.exe -std=c++11 -lstdc++
and produce a working output.
But I have been unsuccessful in finding a way to convert this into a CMAKE file. Does anyone have any resources or even examples of a working example?
Here is an example of my non-working file:
cmake_minimum_required(VERSION 3.11)
SET (PROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
SET(PROJECT_NAME "WxWidgets")
SET(CMAKE_C_COMPILER i686-w64-mingw32.static)
project(
${PROJECT_NAME}
VERSION 1.0
LANGUAGES CXX)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} (i686-w64-mingw32.static-wx-config --cxxflags --libs) -L /home/kevin/wxWidgets/include/ -Wall -std=gnu++2a -static -g")
file(GLOB all_SRCS
"${PROJECT_SOURCE_DIR}/src/*.h"
"${PROJECT_SOURCE_DIR}/src/*.hpp"
"${PROJECT_SOURCE_DIR}/src/*.c"
"${PROJECT_SOURCE_DIR}/src/*.cpp"
"${PROJECT_SOURCE_DIR}/src/**/*.c"
"${PROJECT_SOURCE_DIR}/src/**/*.cpp"
"${PROJECT_SOURCE_DIR}/src/**/*.hpp"
)
add_executable(${PROJECT_NAME} ${all_SRCS})
I am running the command:
i686-w64-mingw32.static-cmake ..
within my build directory.
By removing:
**(i686-w64-mingw32.static-wx-config --cxxflags --libs) -L /home/kevin/wxWidgets/include/**
...I am getting the error:
fatal error: wx/wxprec.h: No such file or directory
8 | #include "wx/wxprec.h"
I have this file I checked with locate:
$ locate wx/wxprec.h
/home/kevin/mxe/usr/i686-w64-mingw32.static/include/wx-3.1/wx/wxprec.h
/home/kevin/wxWidgets/include/wx/wxprec.h
/usr/local/include/wx-3.3/wx/wxprec.h
EDIT:
Thanks to #vre's comment, I have updated my CMakeLists.txt file and have moved closer, it seems now that I just need to link correctly Here is what I have:
cmake_minimum_required(VERSION 3.11)
SET (PROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
SET(PROJECT_NAME "WxWidgets")
SET(CMAKE_C_COMPILER i686-w64-mingw32.static)
project(
${PROJECT_NAME}
VERSION 1.0)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=gnu++2a -static -g")
file(GLOB all_SRCS
"${PROJECT_SOURCE_DIR}/src/*.h"
"${PROJECT_SOURCE_DIR}/src/*.hpp"
"${PROJECT_SOURCE_DIR}/src/*.c"
"${PROJECT_SOURCE_DIR}/src/*.cpp"
"${PROJECT_SOURCE_DIR}/src/**/*.c"
"${PROJECT_SOURCE_DIR}/src/**/*.cpp"
"${PROJECT_SOURCE_DIR}/src/**/*.hpp"
)
add_executable(${PROJECT_NAME} ${all_SRCS})
include( "${wxWidgets_USE_FILE}" )
find_package(wxWidgets REQUIRED)
target_include_directories(${PROJECT_NAME} PUBLIC /home/kevin/mxe/usr/i686-w64-mingw32.static/include/wx-3.1/ /home/kevin/mxe/usr/i686-w64-mingw32.static/lib/wx/include/i686-w64-mingw32.static-msw-unicode-static-3.1/)
target_link_directories(${PROJECT_NAME} PRIVATE /home/kevin/mxe/usr/i686-w64-mingw32.static/lib/)
But sadly, I have many linker errors such as:
undefined reference to `wxFrame::GetDefaultIcon() const'
I have an issue with linking FreeType with my project. I have my libs in a separate folder, {PROJECT}/lib, where libfreetype.a is located. My compiler is MinGW-64 4.8.3 x86_64-posix-seh-rev2, and I built libfreetype.a from source using this compiler. My cmake file looks like the following:
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/Build/Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lmingw32")
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)
set(FREETYPE_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/include/)
set(FREETYPE_LIBRARY ${PROJECT_SOURCE_DIR}/lib/libfreetype.a)
find_package(Freetype REQUIRED)
set(SOURCE_FILES main.cpp ...)
add_executable(Wahoo ${SOURCE_FILES})
target_link_libraries(Test freetype)
Running this will cause the linker to fail with undefined references to '__imp_FT_Init_FreeType'. Removing the the libfreeType.a causes CMAKE say that it cannot find -lfreeType (as expected), but including libfreetype.a will cause the linker errors.
I have to change my answer, because I tried it on my own and it was bullshit.
I think, you have to change only your last line into
target_link_libraries(Test ${FREETYPE_LIBRARY})
I'm trying to build my project using curl, but I have this result :
undefined reference to `_imp__curl_easy_init'
This is my CMakeLists :
cmake_minimum_required(VERSION 2.8)
project(score)
set(SOURCE_FILES main.cpp)
add_executable(score ${SOURCE_FILES})
add_library(libcurl STATIC IMPORTED)
set_property(TARGET libcurl PROPERTY IMPORTED_LOCATION "c:/MinGW/lib")
SET(GCC_COVERAGE_LINK_FLAGS "-lcurl")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
SET( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}" )
Moreover, I put my file libcurl.a, etc... in the correct directory "c:/MinGW/lib".
Could you help me ?
You probably need to compile the source files that call the curl functions with the CURL_STATICLIB macro defined.
Do you have access to the curl-config utility? It's there when you build curl from source. Run it with the --cflags option to get the compiler flags required and the --libs option to get linker requirements.
For example, in my mingw environment, the cflags reported are -DCURL_STATICLIB -I/mingw/local/include and the lib flags reported are -L/mingw/local/lib -lcurl -lssl -lcrypto -lgdi32 -lwldap32 -lz -lws2_32.
I am using these steps (line 42 in the 2nd sourcecode place). However, I reading/writing to files with .h5 extension, where the code needs surely this flag: -lhdf5.
In order to compile the functions for hdf5, I would do something like this:
g++ -std=c++0x main.cpp -lhdf5
Notice that the flag must be placed at the end of the compilation command, as stated in this answer.
I updated my question, due to a comment.
So, I modified the CMakeLists.txt and what I changed was this part:
add_definitions(${CMAKE_CXX_FLAGS} "-std=c++0x")
set(CMAKE_EXE_LINKER_FLAGS "-lhdf5 -lhdf5_hl -lhdf5_cpp")
However, when I execute make, it seems that hdf5 is not found.
EDIT
With Marc's suggestion, I got:
Linking CXX executable exe
/usr/bin/cmake -E cmake_link_script CMakeFiles/exe.dir/link.txt --verbose=1
/usr/bin/c++ -frounding-math -O3 -DNDEBUG -lhdf5 -lhdf5_hl -lhdf5_cpp CMakeFiles/exe.dir/match.cpp.o -o exe -rdynamic -L/home/samaras/code/CGAL-4.3/lib -L/usr/local/lib -lmpfr -lgmp /home/samaras/code/CGAL-4.3/lib/libCGAL.so -lboost_thread-mt -lpthread /usr/local/lib/libboost_system.so /home/samaras/code/CGAL-4.3/lib/libCGAL.so -lboost_thread-mt -lpthread /usr/local/lib/libboost_system.so -Wl,-rpath,/home/samaras/code/CGAL-4.3/lib:/usr/local/lib
and here is the problem, I think (see the answer I linked too). The linker flag of hdf5 is NOT at the end.
How to put it at the end? Maybe I am using the wrong set()?
EDIT - solution
Here is the working CMakeLists.txt:
# Created by the script cgal_create_cmake_script_with_options
# This is the CMake script for compiling a set of CGAL applications.
project( exe )
cmake_minimum_required(VERSION 2.6.2)
if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" VERSION_GREATER 2.6)
if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}.${CMAKE_PATCH_VERSION}" VERSION_GREATER 2.8.3)
cmake_policy(VERSION 2.8.4)
else()
cmake_policy(VERSION 2.6)
endif()
endif()
set( CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true )
if ( COMMAND cmake_policy )
cmake_policy( SET CMP0003 NEW )
endif()
# CGAL and its components
add_definitions(${CMAKE_CXX_FLAGS} "-std=c++0x")
find_package( CGAL QUIET COMPONENTS )
if ( NOT CGAL_FOUND )
message(STATUS "This project requires the CGAL library, and will not be compiled.")
return()
endif()
# include helper file
include( ${CGAL_USE_FILE} )
find_package (CGAL)
include (${CGAL_USE_FILE})
add_definitions (${CGAL_CXX_FLAGS_INIT})
include_directories (${CGAL_INCLUDE_DIRS})
set (libraries ${libraries} ${CGAL_LIBRARY} ${CGAL_3RD_PARTY_LIBRARIES})
set (CMAKE_EXE_LINKER_FLAGS "-dynamic ${CMAKE_EXE_LINKER_FLAGS}")
find_package (HDF5 QUIET COMPONENTS CXX)
if (HDF5_FOUND)
include_directories (SYSTEM ${HDF5_CXX_INCLUDE_DIR})
set (HDF5_libraries ${HDF5_hdf5_LIBRARY} ${HDF5_hdf5_cpp_LIBRARY})
set (HDF5_libraries hdf5 hdf5_cpp)
endif (HDF5_FOUND)
# Boost and its components
find_package( Boost REQUIRED )
if ( NOT Boost_FOUND )
message(STATUS "This project requires the Boost library, and will not be compiled.")
return()
endif()
# include for local directory
# include for local package
# Creating entries for target: exe
# ############################
add_executable( exe match.cpp )
add_to_cached_list( CGAL_EXECUTABLE_TARGETS exe )
# Link the executable to CGAL and third-party libraries
target_link_libraries(exe ${CGAL_LIBRARIES} ${CGAL_3RD_PARTY_LIBRARIES} ${libraries} ${HDF5_libraries})
I use CGAL and HDF5 together. Here's the relevant bit from my CMakeLists.txt:
find_package (HDF5 QUIET COMPONENTS CXX)
if (HDF5_FOUND)
include_directories (SYSTEM ${HDF5_CXX_INCLUDE_DIR})
add_library (hdf5 STATIC IMPORTED)
set_target_properties (hdf5 PROPERTIES IMPORTED_LOCATION ${HDF5_hdf5_LIBRARY})
add_library (hdf5_cpp STATIC IMPORTED)
set_target_properties (hdf5_cpp PROPERTIES IMPORTED_LOCATION ${HDF5_hdf5_cpp_LIBRARY})
set (HDF5_libraries hdf5 hdf5_cpp)
add_executable (exe match.cpp)
target_link_libraries (exe ${libraries} ${HDF5_libraries})
endif (HDF5_FOUND)
Earlier in CMakeLists.txt, there is:
find_package (CGAL)
include (${CGAL_USE_FILE})
add_definitions (${CGAL_CXX_FLAGS_INIT})
include_directories (${CGAL_INCLUDE_DIRS})
set (libraries ${libraries} ${CGAL_LIBRARY} ${CGAL_3RD_PARTY_LIBRARIES})
set (CMAKE_EXE_LINKER_FLAGS "-dynamic ${CMAKE_EXE_LINKER_FLAGS}")
Hi I have problem with linkg Glfw and other libraries using cmake.
From command line i compile like this
g++ main.cpp -lGL -lGLU -lGLEW -lglfw
But I wanted to use cmake for compiling. I tried to use target_linkg_libraries but this produce error
CMake Error at CMakeLists.txt:18 (target_link_libraries): Cannot
specify link libraries for target "GL" which is not built by this
project.
I tried do this using add definitions. I dont see error but this don't link libraries.
cmake_minimum_required (VERSION 2.6)
project (test)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
ADD_DEFINITIONS(
-lGL
-lGLU
-lGLEW
-lglfw
)
add_executable(test.out
main.cpp
)
target_link_libraries(GL GLU GLEW glfw)
The syntax for target_link_libraries is:
target_link_libraries(your_executable_name libraries_list)
And you don't have to add add_definition statements (target_link_libraries adds this options)
There are also some useful variables provided by OpenGL and GLEW packages.
Your CMakeLists.txt should be like:
cmake_minimum_required (VERSION 2.6)
project (test)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR} ${GLEW_INCLUDE_DIRS})
add_executable(test
main.cpp
)
target_link_libraries(test ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES})
One important detail to keep in mind is to place the target_link_libraries after the add_executable (or add_library) line.