Cross Compile C++ program from Windows to Raspberry Pi 4 - c++

I am trying to compile a simple program made in C++ from windows to my raspberry pi 4 B.
For that I use Cmake but I don't know what to use to compile the program. Visual Studio it seems can compile Arm and Arm64 but according to the documentation, the Rp4 is in ArmV8 and when I do uname -m on the Rp4 I get armv71. I don't know what to indicate in CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)
set(CMAKE_SYSROOT /home/devel/rasp-pi-rootfs)
set(CMAKE_STAGING_PREFIX /home/devel/stage)
set(tools /home/devel/gcc-4.7-linaro-rpi-gnueabihf)
set(CMAKE_C_COMPILER ${tools}/bin/arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER ${tools}/bin/arm-linux-gnueabihf-g++)
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)
project(SCWWS CXX)
#=================== INCLUSION OF Project Files ====================#
set(FORMS_DIR "${CMAKE_SOURCE_DIR}/forms")
set(INCLUDE_DIR "${CMAKE_SOURCE_DIR}/include")
set(SOURCE_DIR "${CMAKE_SOURCE_DIR}/src")
include_directories(${FORMS_DIR})
include_directories(${INCLUDE_DIR})
include_directories(${SOURCE_DIR})
file(GLOB_RECURSE SOURCES
"${INCLUDE_DIR}/*.h"
"${SOURCE_DIR}/*.cpp"
)
#=================== SETUP EXECTUABLE ====================#
# Add the executable
add_executable(SCWWS ${SOURCES})
# Add the target includes for SCWWS
target_include_directories(SCWWS PRIVATE ${FORMS_DIR})
target_include_directories(SCWWS PRIVATE ${INCLUDE_DIR})
target_include_directories(SCWWS PRIVATE ${SOURCE_DIR})
When I run my program on the Rp4 I get this error:
-bash: ./SCWWS: cannot execute binary file: Exec format error
For now, here is the cpp code that I am compiling:
#include <iostream>
using namespace std;
int main(){
cout << "Hello World !";
return 0;
}
For later, I would like to make a Qt application, will there be anything else to do?

Related

Export as 32-bit with CMake & C++ won't work

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

Compiling Chaiscript with i686-w64-mingw32-g++ fails

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.

Cross-compile SDL2 from linux to Windows doesn't make an exe

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.

Error when I try to compile project using CMake with SFML library by mingw

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.

CMake variable persisting between runs even though it was reset

I'm trying to cross compile from Ubuntu to Windows, using CMake. I have everything configured (Installed both mingw32 and mingw64), and I have the relevant toolchain files for both. The problem is that a variable that I set in one toolchain file, after it is used, persists in the second run with the other toolchain file one as well.
These are my toolchain files:
Windows 64 bit: (Toolchain-Ubuntu-mingw64.cmake)
set(CMAKE_SYSTEM_NAME Windows)
unset(PROCESSOR_ARCHITECTURE CACHE)
set(PROCESSOR_ARCHITECTURE x64)
message("===Processor architecture: " ${PROCESSOR_ARCHITECTURE})
set(TOOLCHAIN_PREFIX x86_64-w64-mingw32)
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)
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)
Windows 32 bit: (Toolchain-Ubuntu-mingw32.cmake)
set(CMAKE_SYSTEM_NAME Windows)
unset(PROCESSOR_ARCHITECTURE CACHE)
set(PROCESSOR_ARCHITECTURE x86)
message("===Processor architecture: " ${PROCESSOR_ARCHITECTURE})
set(TOOLCHAIN_PREFIX i686-w64-mingw32)
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)
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)
And this is the top of my CMakeLists.txt file:
cmake_minimum_required(VERSION 2.8)
project("project")
set(SOURCE_FILE_LIST main.c)
set(ARCH_PROJECT_NAME ${PROJECT_NAME}${PROCESSOR_ARCHITECTURE})
message("Project - " ${ARCH_PROJECT_NAME})
...
When I run
cmake -DCMAKE_TOOLCHAIN_FILE=../cmake-toolchains/Toolchain-Ubuntu-mingw64.cmake ../src/
The output starts with:
===Processor architecture: x64
Project - projectx64
The unexpected part comes when I run:
cmake -DCMAKE_TOOLCHAIN_FILE=../cmake-toolchains/Toolchain-Ubuntu-mingw32.cmake ../src/
and the output still starts with
===Processor architecture: x64
Project - projectx64
What can I do to fix this behaviour?
CMake will cache many of the values it finds during the setup process. Frustratingly it does this to the point that attempting to recreate the projects with a new option specified on the command line simply doesn't work. The only solution I've found is to either delete the entire build directory, or the specific cache file in the build directory, called CMakeCache.txt I believe.