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

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

Related

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 C++ program from Windows to Raspberry Pi 4

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?

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.

gcc link error:cmake cross compile can't find secondary libraries even if the rpath is set using cmake

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

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.