Sharing a C++ program between OSX and Linux? - c++

I've written a relatively small C++ program in CLion, on a Mac. CLion uses CMake to compile an executable file which can only be run on my Apple machine, I know this much.
My project includes the 'ncurses' library, but other than that it only uses standard C++ ones. My question is, how do I go about running my program on another laptop, running Linux? What is the standard way of deploying C++ applications between platforms, at least between Linux and OSX which both come with a C/C++ compiler? Must CMake exist on both machines for this to be done?
Sorry for the very general question, I've been learning how to write code but not really how to go about sharing it!

To those in a similar situation, I did the following to share my code between Mac and Linux.
As I said, CLion uses CMake. I thought there would be a lot going on under the hood but it turns out all you need to compile your source files is the CMakeLists file, and an installation of CMake itself.
CLion generates the CMakeLists file, but it can be written from scratch and it only needs around 5 lines to begin compiling. In it, you declare the source files you wish to include in compilation, and a few other things, like minimum CMake version and the version of C++ your project uses.
There's a good explanation of what goes into the CMakeLists file here.
My Linux machine runs Ubuntu, and the version of CMake I was using was 2.8.something - quite early compared to 3.6 on my Mac. So firstly this meant I had to change cmake_minimum_required(VERSION 3.6) to 2.8.
Then I tried running cmake . in the directory containing the source files, which threw up a load of compile errors to the terminal. Most of which concerned things like template classes and curly brace initialisation - features of more recent versions of C++.
This was because CMake's set(CMAKE_CXX_STANDARD 11) syntax, which sets the project's C++ version, wasn't around in CMake 2.8. Replacing this line with set (CMAKE_CXX_FLAGS "-std=c++11") fixed this issue.
Another cmake . worked just fine, which then spat out a load of files into the source directory. I'm not entirely sure what all of them do, but they all surround a generated 'Makefile'. Running make in the same directory compiles all of the source files and outputs an executable, which worked perfectly.
The ncurses library seems pretty baked-in to Unix-like/based systems, so I was able to use and compile this library by simply #includeing it in my code. 3rd party libraries might require a bit more work!

Related

Is it ok for CMake to use c++, rather than mpicxx, to compile my code?

I'm trying to build an executable from C++ source code which uses MPI, on a GNU/Linux Devuan Chimaera system. Now, I'm an MPI/OpenMP newbie, I'm just trying to adapt this code, which isn't mine, to be built with CMake - when before it had a Makefile. My build succeeds, but I'm seeing segfaults, so I want to make sure my problem isn't with the build phase, which bugs me.
My CMakeLists.txt has:
find_package(OpenMP REQUIRED)
find_package(MPI REQUIRED)
and my system has OpenMPI 4.1.1 installed, which is found. I do this for my target:
target_link_libraries(my_executable PRIVATE MPI::MPI_CXX OpenMP::OpenMP_CXX)
but nothing else which indicates its expecting to be compiled by mpicxx.
... and indeed, when I configure (with CMake 3.22.1) and then build, the usual c++ executable gets invoked to compile (and then link) the my_target executable.
Questions:
Can source code which originally was getting compiled with mpicxx be compiled with "just" a C++ compiler, with the appropriate includes?
Assuming there's any merit to using mpicxx for compilation - how do I get CMake to use it for my target?
Edit: It's been suggested to me to try using mpirun to run my program. With it, I get no segmentation faults, consistently; it's only when I run directly that I see them.
Can source code which originally was getting compiled with mpicxx be compiled with "just" a C++ compiler, with the appropriate includes?
Yes, and you're doing it correctly.
The MPI systems I've used (Cori, Perlmutter, Stampede) have all provided implementations that work correctly with CMake's MPI support. However, it's possible that a sufficiently poorly administered system will break this.
Assuming there's any merit to using mpicxx for compilation - how do I get CMake to use it for my target?
This is a toolchain setting... set CMAKE_CXX_COMPILER to /path/to/mpicxx either at the command line, in a preset, or in a toolchain file.

fix: too many sections (52187) - Cmake, Qmake

I need to compile a C++ program on Windows using the CLion IDE with CMake and Qt Creator with QMake.
On Linux, I do not get any problems at all.
On Windows, I wanted to use the MinGW compiler. My source code relies on boost. Therefore, according to this question, I was installing the compiler from the MinGW Distro.
So far so good. Now, during the compilation process I get the following error:
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/as.exe: CMakeFiles\[...]: too many sections (52187)
C:\Users\MATTHI~1\AppData\Local\Temp\cc2TyXHu.s: Assembler messages:
C:\Users\MATTHI~1\AppData\Local\Temp\cc2TyXHu.s: Fatal error: can't write 231 bytes to section .text of CMakeFiles\[...]: 'file too big'
after some research, (here and here) I found out that heavy templating might be the reason for this issue... However, I do not do any templating at all, but I'm including the Eigen libraries that might cause the issue.
In any case, I need to fix this and the proposed solution is to add the following flags to my compiler:
-Wa,-mbig-obj.
My question now is: how can I do this in my IDE (CLion or QtCreator) or in my CMakeLists.txt (CMake) and/or myProject.pro (QMake) files?

How to build c++ with all dependencies to run on another machine

I wrote c++ code that in including some libraries that I included using -l option. The code is right on my machine but I want finally run this code on another machine named B. I built it on my machine using c++11 and GNU GCC Compiler and attempt to run it on machine B but it errors :
error while loading shared libraries: libcppkafka.so.0.1: cannot open shared object file: No such file or directory
How can I build c++ code with all dependencies to disappear this error?
note: libcppkafka.so.0.1 is in my machine in path /usr/local/lib
note: I use codeblock IDE, so I appreciate that if solution will be codeblock compatible
note: Both machines are ubuntu 16.04
In order to achieve your goal you have 2 options.
You can copy your shared libraries(libcppkafka.so) with your executable and configure its location correctly.
Or you can avoid shared libraries by statically linking them to your program. For this you'll need to have static version of those libraries (libcppkafka in your case)
Since both machines are running the same distribution and version (Ubuntu 16.04), you could find out on the first machine the installed and useful packages, and install these on the second machine.
You'll need to copy the non-packaged things, e.g. in (some part of) /usr/local/lib
You could consider making a real .deb package for your thing (but that is more work).
Notice that an IDE is just an IDE and don't compile anything (an IDE is running external compiler commands; your compiler is GCC invoked as g++). You should perhaps compile on the command line (and you could even make that some shell script, to run on the other machine).

Programming language that doesn't require a runtime/dependency to be installed

I want to know a programming language that doesn't require a runtime/dependency to be installed on the target system. My primary target is Windows XP and above.
I tried Autohotkey but it dosent have many advance functions.
Firstly, please confirm that does 'C++' requires to install a runtime/dependency on the target system is is Win XP or later. Secondly, please suggest me an alternative to C++ that doesnt require a dependency to be installed.
UPDATE: I will be using CodeBlocks! Does the C++ code compiled with that requires a dependency?
UPDATE: Sorry for the misconception, by CodeBlocks I mean the default compiler of CodeBlocks (ie: GNU GCC Compiler or MinGW).
Everything usually depends on the project, not the language. For example, programs compiled in Visual Studio's C++ uses some runtime libraries to work properly. However, you can configure the project in such way, that these libraries are included in the executable file, thus not needing additional dependencies. Delphi works similarly.
Here's the setting for Visual Studio Project:
If you choose option with "DLL", your program will require runtime DLLs. Otherwise it will be standalone, the runtimes will be incorporated into your binary file.
Edit: In response to question edit
I'll repeat myself: it depends on project, not the compiler or IDE.
If you want to create a program that does not require anything else in order to run, except for base operating system (no .NET, no Java, no Perl, no runtime libraries, etc), then your best bet is to use C or C++ and compile your program as single statically compiled executable.
It is rather difficult to achieve in practice, but it can be done.
Codeblocks is not a compiler, but an IDE, that can use different compilers.
The most common one is MinGW.
To complie with minGW so that all the standard libraries are statically linked you shold configure your project (see "project settings") so the the linker options include the -static flag.
You can even be more specific by stecifying
-static-libgcc
-static-libstdc++

I'm lost in boost libraries (specifically boost_program_options)

Hi all I've been banging my head against the wall all day now.
So I want to move my program onto the university supercomputer, but it doesn't have boost (and I used boost program_options in my code). On my pc, I just have -lboost_program_options and that works fine, but obviously won't work anymore.
So, I need to package the necessary stuff along with my code so that it will compile on the supercomputer (using intel icpc)
My first hurdle was compiling the line in my makefile that had the code that wanted to include the boost header, but I ran the following in my code folder:
bcp --scan --boost=/usr/include/ main.cpp destination_folder/
And put the resulting files in my include directory. which solved that.
Boost program options isn't a header only package unfortunately, so i need something else. I need to get a library or something. Because i get errors when the compiler gets to the last task on my makefile (doing all the object files)
In my travels I found this question:
extractin/building boost program_options
I tried what the answer suggests, but putting "build" in my command doesn't generate any extra files...
Now totally stuck, don't know how to get this library thing. I've read so much stuff on bjam my head is spinning, I just don't have the level of understanding to process it all in my head.
OS: Linux both systems
One option is to build boost on that machine. Install it in your home. Change your CXXFLAGS and LDDFLAGS to point to the proper header and library directories and build your code there.
The other option is to cross compile both on your PC (if you have such a cross toolchain). Link your code statically to boost and take the final binary to the super computer.
Since both systems are linux, you'll just want to use the binaries. If both systems run on the same CPU, just compile your program statically. If not, download the debian package for the architecture your supercomputer runs on and rip headers and binaries from that.
I've build boost from bjam for cross-compiling to windows, and if there ever was a reason to use the autotools in a project, it's the mess of boost and bjam. Avoid it if possible, and try to adapt the debian package source if you can't.
Instead of building Boost.ProgramOptions you could include and compile all its .cpp files within your project.