I am using a WSL running Ubuntu and am trying to get an SDL2 program to compile using the Ubuntu and then run the program on Windows. I have been able to get it to build, however it doesn't make an exe (and changing the file extension to exe doesn't fix it). Also even though I set the CMAKE_CXX_FLAGS to have -o Crawl_The_Dungeon.exe the file still ends up being named all lower case like the project name. I was able to get this to compile and run on linux when I still had a linux machine.
I am still pretty new to CMake so I don't fully understand if I am close at all or not at all.
SET(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_CXX_STANDARD 14) # Enable c++14 standard
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${crawl_the_dungeon_SOURCE_DIR}/CMakePath")
set(TOOLCHAIN_PREFIX x86_64-w64-mingw32)
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX})
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
project(crawl_the_dungeon)
set(SOURCE_FILES final.cpp Soldier.cpp TilesEnum.cpp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --static -std=c++0x")
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
include_directories(${SDL2_INCLUDE_DIR}
${SDL2_IMAGE_INCLUDE_DIR}
${SDL2_TTF_INCLUDE_DIR})
target_link_libraries(crawl_the_dungeon ${SDL2_LIBRARY}
${SDL2_IMAGE_LIBRARIES}
${SDL2_TTF_LIBRARIES}
)
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc")
EDIT: I was dumb to include -o Crawl_The_Dungeon.exe as it now creates an exe. However it says that I can't run the program when opening it.
Related
I have been trying to make a program for Windows with C++ and Linux. I imported some libraries and wrote the code. Now this is the config for CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_C_COMPILER gcc)
set(CMAKE_C_FLAGS -m32)
set(CMAKE_CXX_COMPILER g++)
set(CMAKE_CXX_FLAGS "-std=c++14 -DCMAKE_BUILD_TYPE=release64 -m32")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
project(qpsm)
add_executable(
a.exe
main.cpp
)
add_executable(
a.run
main.cpp
)
I know it's not very efficent, but it does the job for Linux. When it compiles for Windows and I try to open it on a Windows Machine it gives me the error with the title Not supported 16 Bit-Application that a feature called "\??\C:\Users\kroyz\Downloads\a.exe" cannot be run due to a incompatibility with 64-Bit Versions.
I know I am very bad at trying to explain things and errors so let me know if I did something wrong
Regards,
Alex
I am trying to compile a simple chaiscript example for windows from Linux using the i686-w64-mingw32-g++ compiler. I have already gotten it to work with g++, but when compiling my app using the command
i686-w64-mingw32-g++ ./main.cpp -pthread -L/lib/x86_64-linux-gnu/libdl.a -std=c++17
And here are the contents of main.cpp
#include "chaiscript/chaiscript.hpp"
#include "chaiscript/chaiscript_stdlib.hpp"
int main()
{
chaiscript::ChaiScript chai;
chai.eval(R"(puts("Hello"))");
}
This is the error it gives me
How do I fix this error so I can compile my code?
Edit:
I was able to get farther along by using x86_64-w64-mingw32-g++ instead of i686-w64-mingw32-g++, but now I am getting this error:
/usr/bin/x86_64-w64-mingw32-as: /tmp/ccE1ZcKe.o: too many sections (73147)
/tmp/ccwlaMBb.s: Assembler messages:
/tmp/ccwlaMBb.s: Fatal error: can't write 41 bytes to section .text of /tmp/ccE1ZcKe.o: 'file too big'
/usr/bin/x86_64-w64-mingw32-as: /tmp/ccE1ZcKe.o: too many sections (73147)
/tmp/ccwlaMBb.s: Fatal error: can't close /tmp/ccE1ZcKe.o: file too big
Here is the solution using cmake:
First make a toolchain file:
# the name of the target operating system
set(CMAKE_SYSTEM_NAME Windows)
# which compilers to use
set(CMAKE_C_COMPILER x86_64-w64-mingw32-g++)
set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++)
set(CMAKE_FIND_ROOT_PATH /usr/x86_64-w64-mingw32)
# adjust the default behavior of the find commands:
# search headers and libraries in the target environment
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
# search programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
The next step is to make the actual CMakeLists.txt:
# set minimum cmake version
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
# project name and language
project(HelloWorld LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(GNUInstallDirs)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
# define executable and its source file
add_executable(HelloWorld main.cpp)
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
add_definitions(-O3)
SET(CMAKE_CXX_FLAGS "-pthread -Wa,-mbig-obj -static -std=c++17")
target_link_libraries(HelloWorld PUBLIC "-L/lib/x86_64-linux-gnu/libdl.a")
endif()
after, run the following commands to build:
mkdir build
cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=../toolchainfile.cmake
cmake --build .
And you should now have a working exe file for chaiscript
Looks like you need #include <mutex> first. Perhaps you will need others, too.
When I want to compile project using CMake with SFML library by mingw (last verison), I catch a lot of strange errors (c++).
CMake code:
cmake_minimum_required(VERSION 3.0)
project(MLproject)
SET(CMAKE_SYSTEM_NAME Windows)
SET(CMAKE_SYSTEM_VERSION 1)
SET(CMAKE_C_COMPILER "i686-w64-mingw32-gcc")
SET(CMAKE_CXX_COMPILER "i686-w64-mingw32-g++")
SET(CMAKE_RC_COMPILER "i686-w64-mingw32-windres")
SET(CMAKE_RANLIB "i686-w64-mingw32-ranlib")
set(CMAKE_CXX_STANDARD 17)
SET(CMAKE_FIND_ROOT_PATH "/usr/i686-w64-mingw32")
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(SOURCE_EXE src/main.cpp)
set(SOURCE_GAME
src/Boss/Boss.cpp
*...*
src/Game/Game.cpp)
set(SOURCE_UI
src/UI/ScrollBar/ScrollBar.cpp)
add_library(UI SHARED ${SOURCE_UI})
add_library(Game SHARED ${SOURCE_GAME})
add_executable(main ${SOURCE_EXE})
target_link_libraries(main Game UI
sfml-graphics sfml-system sfml-windos sfml-audio)
Error:
CMakeFiles/UI.dir/src/UI/ScrollBar/ScrollBar.cpp.o:ScrollBar.cpp:(.text+0x6d): undefined reference to `_imp___ZN2sf14RectangleShapeC1ERKNS_7Vector2IfEE'
CMakeFiles/UI.dir/src/UI/ScrollBar/ScrollBar.cpp.o:ScrollBar.cpp:(.text+0x399): undefined reference to `_imp___ZN2sf12RenderStates7DefaultE.....
How can I fix it? Does I catch error because have too old C++ version? (Everything is successfully compiled for Linux, but I wanted to make cross-platform)
The error:
CMakeFiles/UI.dir/src/UI/ScrollBar/ScrollBar.cpp.o:ScrollBar.cpp:(.text+0x6d): undefined reference to `_imp___ZN2sf14RectangleShapeC1ERKNS_7Vector2IfEE'
indicates that the UI library uses some SFML components. However, you only link the SFML libraries to the main executable, so the library doesn't know where these SFML references are defined. So, you must link any relevant SFML libraries to the UI target:
target_link_libraries(UI PUBLIC sfml-graphics sfml-system sfml-windos sfml-audio)
Use PUBLIC here to propagate the SFML libraries to consumers of the UI target, such as your main executable. This removes the need to explicitly link them to main. Here is your full CMake with these changes:
cmake_minimum_required(VERSION 3.0)
project(MLproject)
# As commented, consider moving these calls to your toolchain file.
SET(CMAKE_SYSTEM_NAME Windows)
SET(CMAKE_SYSTEM_VERSION 1)
SET(CMAKE_C_COMPILER "i686-w64-mingw32-gcc")
SET(CMAKE_CXX_COMPILER "i686-w64-mingw32-g++")
SET(CMAKE_RC_COMPILER "i686-w64-mingw32-windres")
SET(CMAKE_RANLIB "i686-w64-mingw32-ranlib")
SET(CMAKE_FIND_ROOT_PATH "/usr/i686-w64-mingw32")
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_CXX_STANDARD 17)
set(SOURCE_EXE src/main.cpp)
set(SOURCE_GAME
src/Boss/Boss.cpp
*...*
src/Game/Game.cpp)
set(SOURCE_UI
src/UI/ScrollBar/ScrollBar.cpp)
add_library(UI SHARED ${SOURCE_UI})
# Link SFML libraries to UI.
target_link_libraries(UI PUBLIC sfml-graphics sfml-system sfml-windos sfml-audio)
add_library(Game SHARED ${SOURCE_GAME})
add_executable(main ${SOURCE_EXE})
# Link Game and UI to the executable, which propagates the SFML libraries from UI also.
target_link_libraries(main PRIVATE Game UI)
Note, many of the calls at the top of your CMake belong in a toolchain file (as commented), often for cross-compiling. I encourage you to read through the documentation to determine your need for it, and understand how to set it up.
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.
I'm trying cross compile arm program which depends on lots libraries. Explicit include the lib*.so can solve this but its not decent and in my project is almost impossible.
simplified problem is like below, e.g.
mainexe -> libcv_highgui.so(founded) -> libjpeg.so(missing)
but at linking time, i got error:
ld: warning: libjpeg.so.8, needed by /opt/tkfs/usr/local/lib/libopencv_highgui.so.2.4.10, not found (try using -rpath or -rpath-link)
I have added rpath, and I checked ./build/CMakeFiles/mainexe.dir/link.txt, rpath is already there:
arm-linux-gnueabihf-g++ main.cpp.o -Wl,-rpath,/opt/tkfs/:/opt/tkfs/usr/lib/arm-linux-gnueabihf /opt/tkfs/usr/local/lib/libopencv_core.so.2.4.10 -Wl,-rpath-link,/opt/tkfs/usr/local/lib
here is my cmake file:
cmake_minimum_required(VERSION 3.5)
project(testopencv)
set(CMAKE_EXPORT_COMPILE_COMMANDS "ON")
SET(CMAKE_SYSTEM_NAME Linux)
SET(ROOTFS_DIR "/opt/tkfs/")
set(CMAKE_CXX_COMPILER "arm-linux-gnueabihf-g++")
set(CMAKE_C_COMPILER "arm-linux-gnueabihf-gcc")
set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_FIND_ROOT_PATH
${ROOTFS_DIR}
${ROOTFS_DIR}/usr/lib/
${ROOTFS_DIR}/usr/lib/arm-linux-gnueabihf
)
set(CMAKE_INSTALL_RPATH ${ROOTFS_DIR};${ROOTFS_DIR}/usr/lib/arm-linux-gnueabihf;${ROOTFS_DIR}/opt/ros/indigo/lib)
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
#set(CMAKE_SYSROOT "${ROOTFS_DIR}/")
find_package( OpenCV COMPONENTS core highgui)
add_executable( main src/main.cpp )
target_link_libraries(main ${OpenCV_LIBS})
env:
all ld_library_path is set to empty;
3 gcc version is identical 4.8.4(host, cross compile, arm dev board)
both machine is ubuntu 14.04.
all libs like libjpeg.so.8 are in $rootfs/usr/lib/arm-linux-gnueabihf