I'm lost in boost libraries (specifically boost_program_options) - c++

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.

Related

Sharing a C++ program between OSX and Linux?

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!

How can I compile a C++ project (with g++) to use on other computers?

This may be obvious, but I want to make sure what to do before I do anything rash. I want to compile my C++ program, libraries and all, to a release executable such that the file can be run on any computer (running the same OS). Right now, I'm on Mac OS X (10.7.4) and I need to be able to run my executable on other Macs. The problem is I am using the OpenCV library in my project, and I only have it installed on this computer. Is there a way to compile with g++ such that if I open this program on a computer that doesn't have the OpenCV library installed, it will work anyway? As in, build all the dependencies into the executable. Or does this happen automatically?
I am also quite new to the ".o" object files, so can those have anything to do with it? I would prefer a way to get it all into a single file, but I'll settle for a package as long as it works.
Thank you.
To expand on molbdnilo's answer, you'll need to create an application bundle (see the Apple Bundle Programming guide). You'll need to move your console application to MyApp.app/Contents/MacOS/MyApp. There's also a Frameworks directory in which you'll need to add the OpenCV library as a framework. See the OpenCV Wiki for some information on the OpenCV framework. A framework (at its simplest) is pretty much a dynamic library wrapped in a particular directory structure.
I would suggest looking into using Xcode on the mac as it simplifies the construction of bundles and linking to frameworks compared to doing it yourself via scripting and Makefiles.
There are two ways to do this. You can static link if you aren't going to run into licensing issues with any of the libraries you are linking to. This is pretty easily handled by using g++ -o myApp -static -lopencv myapp.cpp However, this also depends on static libraries existing for the libraries you want to link to. Most distribute static libs with the shared libs these days.
The other way is to distribute the shared libraries and tell your application to force it to look in a certain spot for the shared library using -rpath. Note: I am telling you the Linux way to do this, it will probably work on a Mac but I have no way to test.
So say all of your shared libraries are in the same directory as your executable, you can compile with: g++ -rpath ./ -lopencv -o YourApp yourApp.cpp
I hope this helps.

Using 3rd Party Libraries in C++

I'm totally spinning my wheels with getting a couple of 3rd party libraries to work with my c++ programs. I'm looking for some general advice (40,000 foot level) about the general steps that one needs to take when implementing libraries.
First, some specifics: I am using code::blocks in Windows as my IDE. I like this IDE and really don't want to switch to anything else if I don't have to (I've tried visual c++ and also some things in linux). The libraries that I am trying to use are GMP and crypto++.
OK. What I think I know is this: After downloading the library, I unzip the file to a folder. I've been unzipping directly to C:\ with each zip file extracted to its own folder (e.g. c:\cryptopp and c:\gmp). I think that the next step is to build the library, but this is where I get totally stuck. How is this done? There are no executable files among those extracted. From what I can tell, I believe that I do this in code::blocks, but I have no idea how?
Finally, assuming that I can get this done, which I believe creates the .lib files, the last step before actually using the library in my code, is to link into the library. This part, I believe that I understand.
So, my question is broad: do I understand this process overall? And if so, how do I go about building these libraries, if in fact that it the thing that I am missing.
Thanks very much for indulging my ignorance. I'm totally rudderless right now and despite hours and hours on google, I'm making no progress. Also, feel free to correct anything that I have stated as fact that is not correct. Thanks a lot!
Usually libraries have a special file called makefile in them, and are built with a utility called Make (or one of it's variations, whatever works uder windows).
Usually all you have to do is to run Make in the directory where you have unpacked the source files, and it will do the rest itself.
If those libraries you mention (GMP and crypto++; disclaimer: I'm not familiar with either of them) don't have project files for code::blocks then you may still be able to compile them under Windows with MinGW.
If you have installed MinGW you use the MinGW shell to navigate to the appropriate directories which would be /c/cryptopp/ and /c/gmp in your examples - The MinGW shell works like a Unix shell, and has different naming conventions.
Then you need to compile the libraries. Check whether there's a Makefile in those directories, if there isn't you can check whether there's a configure script, which will generate the Makefile. If you have the Makefile you can type make which will compile the libraries with MinGW's port of the GCC compiler.
When the compilation is complete you should have a library in the form of a .a file (say libcryptopp.a) that you can link to your project. In code::blocks you would set the linker path (the -L command line option in GCC) to C:\cryptopp\bin or wherever the library has been compiled, and then add libcryptopp.a to the list of libraries you want to link (this is associated with the -l option in GCC). The convention is to leave out the lib prefix and the .a extension, so you would just add cryptopp to your library list. In the end your linker options should look like -LC:\cryptopp\bin -lcryptopp along with the
Also, to be able to use the libraries you need to add the path to the headers directory to the include path of your project. This is associated to the -I command line option in GCC, so your compiler's command line options would have something like -IC:\cryptopp\include somewhere.
Of course, all of the above assumes that you use code::blocks with GCC. If you're using it with VisualC++ then the principles are the same, but the specific steps differ.

Boost C++ libraries installation

I have just downloaded the boost libraries from the boost website and extracted them to my desktop. I was hoping to just have a quick look at them and have them installed on my machine and perhaps use them in the future when I am more accustomed to C++.
When I extracted it, I was confused with all of the extracted files. There is all of the headers in the boost directory but tutorials mention running bootstrap.bat (I'm using Windows).
So I am asking this: do I simply extract the headers to my compilers include directory like normal to get boost up and running or do I need to do something else?
As I understand it from searching about, apparently "most" of boost is just templates and can be used simply by including the headers, but what about the rest?
Am I totally barking up the wrong tree?
Thanks for any help
Since you mentioned you run Windows, take a look at this automated installer:
► http://www.boostpro.com/download/
Also, some general advice:
do I simply extract the headers to my compilers include directory
No! Do not pollute your compiler's includes with third-party includes; make a separate directory specifically for a particular library. You'll then need to tell your specific IDE in what directory it can find the library headers.
I usually use boostpro's installer, it is less work. I vaguely remember having to set up the BOOST_ROOT environment variable on one of my systems to use it.
The libraries that contained compiled source should be included in the installer.
If you don't use the installer (or don't set up your build correctly), and try to use the libraries that need it you will likely get some linker errors when you try and compile your program. Usually if you take those linker errors and plop them in google it tells you pretty quick which libraries you need to include in your build system. I use CMake for that and have been very happy..
Just add the root boost directory to include paths of your compiler/IDE (so if you have Boost extracted to C:\Boost, the compiler will use that path, not C:\Boost\boost).
Don't do any copying of the boost folder to your compiler's include directory, because it may make upgrading Boost harder in the future.
Also if you plan to use any of boost's compiled libraries, add Boost's lib directory to compiler's library search paths. Configuring the compiling step is a matter of putting the right toolset parameter to boost's build tool. If you have your command line environment configured properly, bootstrap should run and compile the builder without any problems, and the Boost builder should properly detect your toolset, so no parameters will be necessary.
As you do such configuration only once every time you do a clean install of your favorite compiler, it's not as hard or daunting as it seems.

Trouble with boost and Code::Blocks

I've been trying to get the boost library working with Code::Blocks and am having some trouble.
When I first tried to get boost, all I did was download the latest zip file and extract it into the CodeBlocks folder. Then I set the compiler settings to look in the boost folder. This allowed me to compile, but not to link. I then read the BoostWindowsQuickReference. I removed everything I had related to boost from my machine, and started fresh.
I followed the instructions step by step, and the only thing that didn't go exactly as the instructions said was that the install-proper folder with the include\boost inside of it was in C: and not my CodeBlocks folder. So I simply copied it (just in case for some reason it needed to be there) to the CodeBlocks folder, which I thought odd because I already had a boost_1_40_0 folder there from downloading the .zip.
I then tried to compile a program and it came up with the exact same error. Then I realized that I forgot to put in the link library (Ex: boost_regex-mgw44-mt-1_40.lib). Now I get
error: ld.exe||cannot find -lboost_regex-mgw44-mt-1_40.lib|
I have a few questions:
Obviously, what am I doing wrong?
Will I need to put in a link library every time I want to use a boost facility (everything is separated into different files, there isn't just one big project.)
Was it necessary to build the library with the boost-jam or could I have just extracted it and used it? (Probably a dumb question, but a small seed of doubt was planted when I got the exact same error.)
Should I try nuwen's MinGW Distro? (Would it make things any easier?)
If any clarification is needed I'd be happy to do so. Thanks.
Edit: and now I can't compile regular programs. So I'm just starting fresh again.
1, it should be -lboost_regex-mgw44-mt-1_40
2, Read the document, most boost library doesn't require to link library
3,4, You should compile it yourself, or try nuwen's MinGW ( I'd installed it and it worked fine )
BoostPro has Windows binaries available for the Boost libraries. If you download just the Boost sources, you will have to compile it, if you are using any of the libraries that are not header-only (such as boost regex). The BoostPro binaries will allow you to link to these without having to build anything.
On Windows it doesn't really matter where you "install" Boost to. Just get the .7z, compile using bjam.exe and pass it the options you need. It will create a folder called "bin.v2" and put the resulting libs in there. In Code::Blocks, all you need to do is edit the project options and point the search path to boost_1_40_0\boost and manually input the libraries to link against (those from bin.v2). It should just work then.
Do not use a precompiled Boost library.