Compiling SFML program with CMake on windows - c++

For the past few days I've been searching everywhere and can't seem to fix this problem or maybe I just didn't understand some answers.
I have an application (a game to be precise) built with SFML that I managed to compile on Linux with 2.4.2 version. I want to migrate to 2.5.1 and be able to compile on Windows so I changed the CMakeLists.txt file to this:
cmake_minimum_required(VERSION 3.0)
# There is no real need for C++17 or 14, but I like to use up-to-date versions
if(CMAKE_MAJOR_VERSION VERSION_GREATER 3.8.0)
set(CMAKE_CXX_STANDARD 17)
else()
set(CMAKE_CXX_STANDARD 14)
endif(CMAKE_MAJOR_VERSION VERSION_GREATER 3.8.0)
if (WIN32)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Binaries/)
endif(WIN32)
# Includes
include_directories(include)
# Source files
set(SOURCE_FILES
source/main.cpp
source/Game.cpp
)
# Assets
set(ASSETS
assets/Fonts/xolonium.ttf
)
# Debug Mode
project(HexDebug VERSION 1.0 LANGUAGES CXX)
set(CMAKE_BUILD_TYPE DEBUG) # This is ignored by MSVC
#SFML Package
find_package(SFML 2.5 CONFIG COMPONENTS system window graphics network audio REQUIRED)
if(SFML_FOUND)
set(SFML_STATIC_LIBRARIES TRUE)
if(WIN32)
set(SFML_USE_STATIC_STD_LIBS TRUE)
endif(WIN32)
endif(SFML_FOUND)
if(UNIX)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Binaries/Debug/)
endif(UNIX)
foreach(asset ${ASSETS})
configure_file(${asset} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${asset} COPYONLY)
endforeach(asset)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_compile_options(${PROJECT_NAME} PRIVATE -g -Wall -O0 -D_DEBUG)
if(SFML_FOUND)
target_link_libraries(${PROJECT_NAME} sfml-system sfml-window sfml-graphics sfml-network sfml-audio)
endif(SFML_FOUND)
#Release Mode
project(Hex VERSION 1.0 LANGUAGES CXX)
set(CMAKE_BUILD_TYPE RELEASE) # This is ignored by MSVC
if(UNIX)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Binaries/Release/)
endif(UNIX)
foreach(asset ${ASSETS})
configure_file(${asset} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${asset} COPYONLY)
endforeach(asset)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_compile_options(${PROJECT_NAME} PRIVATE "-O3")
if(SFML_FOUND)
target_link_libraries(${PROJECT_NAME} sfml-system sfml-window sfml-graphics sfml-network sfml-audio)
endif(SFML_FOUND)
The cmake works and generates the VS solution, that I can build with msbuild (note that I don't want to launch Visual Studio, but only use msbuild). But once I try to run it, it asks for dlls and I don't want that.
I thought that would be solved by linking statically (I don't know much about static or dynamic linking) but it still asks for them.
In this link they say that we don't need to include the ${SFML_INCLUDE_DIR} and ${SFML_LIBRARIES} anymore: https://en.sfml-dev.org/forums/index.php?topic=24070.0
How can I avoid that? I want to be completely independant from DLL's or at least be able to include the bin directory installed from SFML compiled and installed binaries (that I compiled, not the precompiled).
Thank you.

Related

How can I make my cmake project on visual studio 2022 targeting wsl2 to compile pthreaded program [duplicate]

I'm running RHEL 5.1 and use gcc.
How I tell cmake to add -pthread to compilation and linking?
#Manuel was part way there. You can add the compiler option as well, like this:
If you have CMake 3.1.0+, this becomes even easier:
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(my_app PRIVATE Threads::Threads)
If you are using CMake 2.8.12+, you can simplify this to:
find_package(Threads REQUIRED)
if(THREADS_HAVE_PTHREAD_ARG)
target_compile_options(my_app PUBLIC "-pthread")
endif()
if(CMAKE_THREAD_LIBS_INIT)
target_link_libraries(my_app "${CMAKE_THREAD_LIBS_INIT}")
endif()
Older CMake versions may require:
find_package(Threads REQUIRED)
if(THREADS_HAVE_PTHREAD_ARG)
set_property(TARGET my_app PROPERTY COMPILE_OPTIONS "-pthread")
set_property(TARGET my_app PROPERTY INTERFACE_COMPILE_OPTIONS "-pthread")
endif()
if(CMAKE_THREAD_LIBS_INIT)
target_link_libraries(my_app "${CMAKE_THREAD_LIBS_INIT}")
endif()
If you want to use one of the first two methods with CMake 3.1+, you will need set(THREADS_PREFER_PTHREAD_FLAG ON) there too.
The following should be clean (using find_package) and work (the find module is called FindThreads):
cmake_minimum_required (VERSION 2.6)
find_package (Threads)
add_executable (myapp main.cpp ...)
target_link_libraries (myapp ${CMAKE_THREAD_LIBS_INIT})
Here is the right anwser:
ADD_EXECUTABLE(your_executable ${source_files})
TARGET_LINK_LIBRARIES( your_executable
pthread
)
equivalent to
-lpthread
target_compile_options solution above is wrong, it won't link the library.
Use:
SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -pthread")
OR
target_link_libraries(XXX PUBLIC pthread)
OR
set_target_properties(XXX PROPERTIES LINK_LIBRARIES -pthread)

How to use intel opencl sdk in Clion in windows 10?

I have installed Intel opencl sdk for windows and opencl variable are added to the environment variables. I want to use this sdk with my Clion ide which I am unable to include it in my current project since it was CL/cl.hpp not found. How can add it to my project in Clion?
Cl/cl.hpp is located atC:\Program Files(x86)\IntelSWTools\OpenCL\sdk\include\CL
following is my CMakeLists.txt
project(tpch_framework)
# enable c++11
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_BUILD_TYPE "Release")
find_package(OpenMP REQUIRED)
find_package(OpenCL REQUIRED)
if (OPENMP_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()
# Configure required Boost libraries
set(BOOST_ROOT "" CACHE PATH "Boost build root (useful on Windows)")
option(Boost_USE_STATIC_LIBS
"Search for static boost libs" OFF)
option(Boost_USE_MULTITHREADED
"Search for multithreaded boost libs" ON)
option(Boost_USE_STATIC_RUNTIME
"Search for boost libs linked against static C++ runtime" OFF)
find_package(Boost 1.47.0 REQUIRED filesystem system)
# ensure that dependant libraries not explicitly specified here
# are found by the linker:
link_directories(${Boost_LIBRARY_DIRS})
include_directories(${Boost_INCLUDE_DIRS})
set(LIBS ${LIBS} ${Boost_LIBRARIES})
#Bring the headers into the project
include_directories(include)
FILE(GLOB_RECURSE INC_ALL "include/*.hpp")
#However, the file(GLOB...) allows for wildcard additions:
file(GLOB SOURCES "src/*.cpp")
add_library(tpch_framework ${SOURCES})
add_executable(framework main.cpp ${INC_ALL})
target_link_libraries(framework tpch_framework)
#target_link_libraries(framework stdc++fs)
target_link_libraries(framework ${LIBS})
You need to provide information about include directory for OpenCL headers like you provided for Boost headers. Also, you need to link OpenCL libraries with your target.
In your CMakeLists...
For include and link directories:
link_directories(${Boost_LIBRARY_DIRS} ${OpenCL_LIBRARY})
include_directories(${Boost_INCLUDE_DIRS} ${OpenCL_INCLUDE_DIRS})
For linking libraries:
set(LIBS ${LIBS} ${Boost_LIBRARIES} ${OpenCL_LIBRARY})

OpenCV 3.2 file compilation?

How to compile c++ file (opencv included) using opencv 3.2, mingw and command window in Windows 10. (I do not want to use any softwares like codeblocks/Vbasic/eclipse/netbeans).
Try using cmake for generating the makefile and then use the same for make and build.
Following is a minimalisitic example of cmake wherein I was using opencv for clustering of pixels.
You need to create a CMakeLists.txt file and add the following things. for more information on building you can check my github repo:
https://github.com/anubhavrohatgi/ImageProcessing.git
#Cmake for Clustering Project
PROJECT(clustering)
#Minimum version of Cmake required
cmake_minimum_required(VERSION 3.1)
IF(NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE Release)
ENDIF(NOT CMAKE_BUILD_TYPE)
#Find and add opencv libraries
find_package(OpenCV REQUIRED core imgproc highgui imgcodecs)
IF(${OpenCV_VERSION} VERSION_LESS 2.4.12)
MESSAGE(FATAL_ERROR "OpenCV version is not compatible : ${OpenCV_VERSION}")
ENDIF()
#Check for C++ Compiler version. I am using C++14.
INCLUDE(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
IF(COMPILER_SUPPORTS_CXX14)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
ELSEIF(COMPILER_SUPPORTS_CXX0X)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
ELSE()
MESSAGE(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++14 support. Please use a different C++ compiler.")
ENDIF()
#include the header files located in the include folder
INCLUDE_DIRECTORIES(include)
#Set the variables for Headers and Sources. Manually add the filenames. This
#is useful if the files are changed, removed or name modified.
#Better than GLOB version
SET( HEADERS
include/clustering.h
)
set( SOURCES
src/main.cpp
)
add_executable(clustering ${SOURCES} ${HEADERS})
target_link_libraries(clustering
${OpenCV_LIBS}
)

LINK : fatal error LNK1181: cannot open input file '\machine:x64.obj'

I am porting a C++ project I have been working on for 2 years, to Windows, and I am facing this link issue, that I haven't seen on any google-referenced posts... :/
It is a CMake-generated project, and I get these CMake entries set by default to "\machine:x64"
CMAKE_EXE_LINKER_FLAGS
CMAKE_MODULE_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS
CMAKE_STATIC_LINKER_FLAGS
I tried removing the flag, which has no effect on any of the 3 last entries, but CMAKE_EXE_LINKER_FLAGS without this value gives me about 85 unresolved externals spread in my libraries.
ALL my projects / project dependencies are compiled with MSVC2015 in x64.
Linking fails both in debug & release mode (not that it matters..)
I should also probably mention that I have 6 projects in this solution, 4 libraries, 2 executables. 4 of these projects are compiling fine (1 executable & 4 libs), only the 2 last ones are failing (with the same link error for both).
The only difference in terms of dependencies are a dependency to the Point Cloud Library (pcl), and to Qt5 (Core GUI, XML, and OpenGL)
Did anyone already encountered this specific linker error?
Hope you can help =)
EDIT: Here are the 2 main CMakelists files (the one at the root of the project and the one that compiles both failing projects). I removed everything that didn't seem pertinent for this issue.
Root CMakeLists.txt:
project(SofaOR)
cmake_minimum_required(VERSION 2.4)
if (COMMAND cmake_policy)
cmake_policy(SET CMP0003 NEW)
endif(COMMAND cmake_policy)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
include_directories(${CMAKE_SOURCE_DIR})
#Reading local file for the non automatically detected dependencies
if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/cmake/dependencies.cmake")
message("Adding custom file")
include(${CMAKE_CURRENT_LIST_DIR}/cmake/dependencies.cmake)
endif()
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
set(SOFA_ALL_LIBS SofaHelper SofaDefaultType)
set(SOFA_INCLUDE_DIRS ${SOFA_DIR}/include)
set(SOFA_LIBRARY_DIRS ${SOFA_DIR}/lib)
add_subdirectory(${CMAKE_SOURCE_DIR}/processOR)
the CMakelists included by the last add_subdirectory:
project(processOR)
cmake_minimum_required(VERSION 3.0.2)
if (COMMAND cmake_policy)
cmake_policy(SET CMP0003 NEW)
endif(COMMAND cmake_policy)
set(PROCESSOR_EXEC_NAME processOR)
set(PROCESSOR_LIB_NAME processOR_lib)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(OpenCV COMPONENTS core imgproc xfeatures2d calib3d REQUIRED )
find_package(Qt5 COMPONENTS Core Gui Xml OpenGL REQUIRED )
find_package(PCL COMPONENTS common io kdtree REQUIRED )
find_package(Boost COMPONENTS program_options chrono thread REQUIRED)
find_package(OpenIGTLink REQUIRED )
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(PROCESSOR_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/src/ PARENT_SCOPE)
set(PROCESSOR_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/)
include_directories(${PCL_INCLUDE_DIRS})
include_directories(${COMMON_INCLUDE_DIRS})
include_directories(${OpenIGTLink_INCLUDE_DIRS})
include_directories(${OpenCV_INCLUDE_DIRS})
include_directories(${QGLVIEWER_DIR})
include_directories(${Boost_INCLUDE_DIRS})
include_directories(${SOFA_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
link_directories(${COMMON_LIBRARY_DIRS})
link_directories(${OpenIGTLink_LIBRARY_DIRS})
link_directories(${OpenCV_LIBRARY_DIRS})
link_directories(${QGLVIEWER_DIR})
link_directories(${SOFA_LIBRARY_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
include_directories(${PROCESSOR_SRC})
set(PROCESSOR_ALL_LIBS
${PCL_LIBRARIES}
${COMMON_LIBRARIES}
OpenIGTLink
${OpenCV_LIBS}
QGLViewer
${Boost_LIBRARIES}
${SOFA_ALL_LIBS}
)
set (HEADER_FILES
${PROCESSOR_SRC}/util/Calibration.h
${PROCESSOR_SRC}/fetchers/VideoFetcher.h
[…]
${PROCESSOR_SRC}/filters/PointCloudTriangulator.h
${PROCESSOR_SRC}/filters/PointCloudSmootherMLS.h
${PROCESSOR_SRC}/filters/PointCloudSegmenter.h
${PROCESSOR_SRC}/filters/PointCloudDownSampler.h
)
set (SOURCE_FILES
${PROCESSOR_SRC}/Registrator.cpp
${PROCESSOR_SRC}/tinyxml2.cpp
[…]
${PROCESSOR_SRC}/filters/PointCloudSegmenter.cpp
${PROCESSOR_SRC}/filters/PointCloudDownSampler.cpp
)
# Library
add_library(${PROCESSOR_LIB_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES})
target_link_libraries(${PROCESSOR_LIB_NAME} ${PROCESSOR_ALL_LIBS} ${CMAKE_EXE_LINKER_FLAGS} ${ADDITIONAL_LIBRARIES})
QT5_USE_MODULES(${PROCESSOR_LIB_NAME} Widgets Gui OpenGL Xml)
set(PROCESSOR_LIB ${PROCESSOR_LIB_NAME} PARENT_SCOPE)
# Main executable
set (SOURCE_FILES
${PROCESSOR_SRC}/mainRegistration.cpp
)
add_executable(${PROCESSOR_EXEC_NAME} ${HEADER_FILES} ${SOURCE_FILES})
target_link_libraries(${PROCESSOR_EXEC_NAME} ${PROCESSOR_LIB} ${Boost_LIBRARIES} ${PROCESSOR_LIB_NAME} ${CMAKE_EXE_LINKER_FLAGS})
QT5_USE_MODULES(${PROCESSOR_EXEC_NAME} Widgets Gui OpenGL Xml)
set_target_properties(${PROJECT_NAME}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)

Using Qt inside Clion

I'm trying to use Clion IDE to compile a simple program using Qt library, but I can't figure out how to configure CMakeLists.txt file. (I'm not familiar with cmake and toolchain)
this is my current CMakeLists.txt file:
cmake_minimum_required(VERSION 3.2)
project(MyTest)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(MyTest ${SOURCE_FILES})
# Define sources and executable
set(EXECUTABLE_NAME "MySFML")
add_executable(${EXECUTABLE_NAME} main.cpp)
# Detect and add SFML
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
find_package(SFML 2 REQUIRED system window graphics network audio)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
endif()
It's configured to use SFML library with a "FindSFML.cmake" file and it works fine. (I have copied these files from some tutorial) now I want some help regarding proper CMakeLists.txt configuration to compile programs that are using Qt library (it's more helpful if the files and explanations are provided).
P.S: my current OS is manjaro 0.8.13 and all I could find was explaining configurations in windows environment so I was unable to implement those tutorials.
In addition to #tomvodi's answer, you can use a simpler syntax :
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui).
Then, you don't call qt5_use_modules but instead use the standard command to link :
target_link_libraries(MyTest Qt5::Core Qt5::Widgets Qt5::Gui)
Your CMake project file is missing the Qt packages. You have to add:
find_package( Qt5Core REQUIRED )
find_package( Qt5Widgets REQUIRED )
find_package( Qt5Gui REQUIRED )
and then
qt5_use_modules( MyTest Core Widgets Gui )