Compiling a 32-bit C++ program in CLion on MacOS - c++

I having trouble with my CMake file in CLion running on Mac OSX 10.14. Currently, I have the following for a 32-bit program I copied from a Linux server using a built "supplied.o" file from an instructor. I wanted to work in CLion and wondering if it is possible to add to CLion to work outside of the Linux server?
cmake_minimum_required(VERSION 3.14)
project(app)
set(CMAKE_CXX_STANDARD 14)
include_directories(.)
add_executable(app
dlist.cpp
dlist.h
main.cpp
supplied.o)
set_target_properties(app PROPERTIES LINKER_LANGUAGE CXX )
set_target_properties(app PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")
I tried poking around here to see if I could figure it out, but it's possible I don't have a 32-bit G++ library (is that i386?) and not quite sure how to go about installing that. I have homebrew and feel comfortable enough with Linux to just work there, but I enjoy CLion for getting quick ideas out. Thanks for any help anyone can provide!

Related

CMake compile cpp project for windows

Good afternoon!
I have a C ++ project in Macos with CLion, it works perfectly but when I compile it only creates the files prepared for Mac (or so I think). I want to transfer the project to a windows so that my colleagues can use it without having a mac. Is it possible to configure the CMakeList so that a file is created that can be used in windows?
I give you the configuration that I have in CMakeList
cmake_minimum_required(VERSION 3.19)
project(serviceToSQL)
set(CMAKE_CXX_STANDARD 14)
add_executable(serviceToSQL main.cpp tinyxml2.cpp)
add_library(tinyxml2 tinyxml2.cpp tinyxml2.h)
add_library(tinyxml2::tinyxml2 ALIAS tinyxml2)
Thank you!

How do I designate which compiler is called when running cmake + make?

I'm trying to compile a c++ project with cmake and make on OSX but it looks like make is using CXX or clang when I want to use g++ (gcc) so I can follow the answer here to tell the compiler where to find header files (#includes) for tbb used in the project: Need help getting intel TBB working?
brew list shows that I have up to date versions of cmake, make, gcc, and swig installed.
Here's the project I'm trying to compile for reference: https://github.com/nmoehrle/mvs-texturing/blob/master/README.md
I came across this related answer and was able to get the project working! MacOS, CMake and OpenMP
I updated the cmakelists.txt with the following commands to set llvm as the compiler. Note, I needed to update the llvm file path to match the version number I have installed.
set(CMAKE_C_COMPILER "/usr/local/Cellar/llvm/5.0.1/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/local/Cellar/llvm/5.0.1/bin/clang++")
set(OPENMP_LIBRARIES "/usr/local/Cellar/llvm/5.0.1/lib")
set(OPENMP_INCLUDES "/usr/local/Cellar/llvm/5.0.1/include")
Then I was also having issues with OpenMP so I added this section to configure the OpenMP include directories and link directories.
if (OPENMP_FOUND)
include_directories("${OPENMP_INCLUDES}")
link_directories("${OPENMP_LIBRARIES}")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif(OPENMP_FOUND)
I'm a novice dev and wish I had more insight into why this worked but it fixed my issues!

LibTorch with CMake via Eclipse in Windows:Terminated exit value 390

I used cmake4eclipse to build torch C++ version 1.0 stable in Windows 10. Basically, I have the following CMakeLists.txt to build the mnist example:
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
project(mnist)
set(CMAKE_PREFIX_PATH "C:/rl/libtorch/share/cmake/Torch")
set(Torch_DIR "C:/rl/libtorch")
find_package(Torch REQUIRED)
option(DOWNLOAD_MNIST "Download the MNIST dataset from the internet" ON)
if (DOWNLOAD_MNIST)
message(STATUS "Downloading MNIST dataset")
execute_process(
COMMAND python ${CMAKE_CURRENT_LIST_DIR}/download_mnist.py
-d ${CMAKE_BINARY_DIR}/data
ERROR_VARIABLE DOWNLOAD_ERROR)
if (DOWNLOAD_ERROR)
message(FATAL_ERROR "Error downloading MNIST dataset: ${DOWNLOAD_ERROR}")
endif()
endif()
set(CMAKE_BUILD_TYPE Debug)
add_executable(mnist mnist.cpp)
target_compile_features(mnist PUBLIC cxx_range_for)
set_property(TARGET mnist PROPERTY CXX_STANDARD 14)
target_link_libraries(mnist ${TORCH_LIBRARIES})
Then, I load this along with the mnist.cpp and download_mnist.py files in a folder and start a project in eclipse IDE for C/C++, version 2018-09 (4.9.0). In project_properties->C/C++ Build->Tool Chain Editor, I set CMake Builder (GNU Make) and select MinGW GCC. Then, in project_properties->C/C++ General->Preprocessor Include Paths Macros etc.->Providers I select CMAKE_EXPORT_COMPILE_COMMANDS Parser [Shared] and move it up, as it is explained here.
Then, I can compile the mnist project without any error. But, when I run it get <terminated> (exit value 390) a.exe [some address]. I tried to debug this code to find out the problem, but I cannot see the debug screen, and instead I get:
Running the debug mode to the end results in a same error.
I can run mnist.cpp in Linux without any problem, though I use cmake -G "Eclipse CDT4 - Unix Makefiles" ./ to create a eclipse project. I did not know how I can use cmake -G "Eclipse CDT4 - Unix Makefiles" ./ in Windows and I used cmake4eclipse and I believe I have missed a step in dealing with the CMakeLists.txt file in windows. I appreciate any help or comments.
Thanks,
Afshin
I asked same question in torch git, and today I got an answer for that. It seems that for now, we will not be able to run Libtorch through Eclipse with MinGw. Here is the answer that I got from torch git page:
"I don't think you could build that with MinGW because the code is written in c++ and MinGW is not abi-compatible with MSVC. So I think you may need to compile with MSVC. And also in MSVC, the configuration debug and release could not be mixed. So you will have to choose Release as we only provide library with the Release configuration."
See more details in:
https://github.com/pytorch/pytorch/issues/15711

Static Compilation for wxWidgets C++

As a starter I would like to point out that I have spent few hours trying to resolve the problem with no success. I wrote a small project using wxWidgets library for C++ in Clion on Mac. Since majority of my classmates have done their projects in VisualStudio on Windows, nobody could help me out. The project must be handed in in a form of an executable which will work on any computer. I suppose that the solution I should be looking for is static compilation which would staticly link all of the libraries to the executable file. I tried adding some flags like the one below to the Cmake:
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lpthread")
but it did not help whatsoever. I have absolutely no knowledge on how CMake works and I would greatly appreciate any help.
The whole cmake file which I currently have:
cmake_minimum_required(VERSION 3.9)
project(Lab3)
set(EXECUTABLE_NAME "Indexer")
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "-g -Wall -Wextra -DWX_PRECOMP")
# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
find_package(wxWidgets COMPONENTS core base REQUIRED)
include(${wxWidgets_USE_FILE})
include_directories(include/ ${wxWidgets_INCLUDE_DIRS})
file(GLOB SOURCES "src/*.cpp")
add_executable(${PROJECT_NAME} main.cpp Source.cpp Source.h)
target_link_libraries(${PROJECT_NAME} ${wxWidgets_LIBRARIES})
EDIT: I guess I didn't specify the problem clearly enough. The program compiles fine- everything works. It's just that whenever I try to copy the executable file (the end application) to a different computer it just doesn't work or shows up as a text file with corrupted characters instead of an application with a GUI etc..
You're not copying the right thing across. What you believe to be the application is probably just some kind of launcher.
Mac GUI applications do not consist of just a single file. Your Indexer app is actually what Apple call a bundle. The Finder presents this to the user as a single entity but it's really a folder.
You can look inside a bundle by right-clicking on it (or Ctrl+click) and selecting Show package contents. If you want to understand a bit more about what's in there, Apple document it here.
A good way to get a bundle from A to B might be to ZIP it. Once you have done that you will find out whether your efforts to statically link it have been successful. What I don't know is where your build system has put it, but maybe this post will help. It will be called Indexer.app, maybe Spotlight can find it.
To build wxWidgets with as few dependencies as possible, you should configure it with not only --disable-shared, but also --disable-sys-libs, to prevent it from using any third party libraries (e.g. libz, libpng, ...) that might be installed on your system.
After building your application using wxWidgets, you should confirm that otool -L YourApp.app/Contents/MacOS/YourApp doesn't show any non-system libraries.

How to use pkg-config in CMake (juCi++)

I've been happily programming in C++ and compiling with g++ for quite a while. Not long ago, I'd decided to get an IDE, and I came accross juCi++.
This IDE is absolutely brilliant, but it uses CMake (or Meson) to build projects. This wasn't a problem, until I had to include a library (GTK+ 3.0 if you're wondering) using pkg-config. This could be done quite easily when compiling with g++, but, as I am completely new to CMake, I have no idea how to do it in the new IDE.
Can somebody please explain?
If your IDE handles CMake and Meson, it should be able to detect your project files. I'd say go for Meson, it's the future, and CMake syntax has a few quirks that Meson doesn't.
Meson:
Meson documentation
He's a basic meson.build that expects to find your application code in main.c and produces a binary named gtk3-test.
project('gtk3-test', 'c')
cc = meson.get_compiler('c')
deps = dependency ('gtk+-3.0')
sources = ['main.c']
executable('gtk3-test', sources, dependencies: deps)
CMake
CMake documentation
For CMake, just give a look at my answer to How do I link gtk library more easily with cmake in windows? (which also works under Linux). It was for GTK+2, but adapting it to GTK+3 is easy, so here's the CMakeLists.txt to use:
project (gtk3-test)
cmake_minimum_required (VERSION 2.4)
find_package (PkgConfig REQUIRED)
pkg_check_modules (GTK3 REQUIRED gtk+-3.0)
include_directories (${GTK3_INCLUDE_DIRS})
link_directories (${GTK3_LIBRARY_DIRS})
add_executable (gtk3-test main.c)
add_definitions (${GTK3_CFLAGS_OTHER})
target_link_libraries (gtk3-test ${GTK3_LIBRARIES})