Trying to include boost serialization in project - c++

I added -lboost_serialization in the makefile:
This is what I include in the main to use boost:
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/binary_object.hpp>
#include <boost/archive/polymorphic_binary_iarchive.hpp>
#include <boost/archive/polymorphic_binary_oarchive.hpp>
when I compile I don't get errors but when I run the program I get this error message:
"./Project: error while loading shared libraries: libboost_serialization.so.1.63.0: cannot open shared object file: No such file or directory"
what should I do to fix this?

When this happens you most likely need to adapt your LD_LIBRARY_PATH. As you need to load shared library, and they are placed at some unusual place, you need to specifiy where they can be found when your program is executed.
I guess in your case they cannot be found in the folder /usr/lib but at some other place, then just add this folder to the mentionned environment variable, using
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/some/new/path
And then start your program as usually.
The path /some/new/path is the path where the file libboost_serialization.so is located.

I had the same problem when I tried to run some script and add the LD_LIBRARY_PATH didn't work... so I simply copied the ".so" file to /usr/lib and it worked perfectly.

Related

How to include third party libraries in C++ windows?

I have the boost library in my downloads folders. When I tried to include a particular file. It is throwing errors. Below is the code and the steps I did.
\main.cpp
#include "type_index.hpp"
int main(){
//some code
return 0;
}
I opened the command prompt and ran the following command
g++ -IC:\Users\Owner\Downloads\boost_1_70_0\boost -o main main.cpp
I got the following error in command prompt
In file included from main.cpp:2:0:
C:\Users\Owner\Downloads\boost_1_70_0\boost/type_index.hpp:17:28: fatal error: boost/config.hpp: No such file or directory
#include <boost/config.hpp>
^
compilation terminated.
How can I run the above file? Do I have to change the location of boost directory from downloads folder to some where within mingw directories?
Adding the picture of directory:
Assuming boost is correctly configured and built on your system, there will be a location where the hub of the boost include root is located. Ex: if you downloaded and built boost in c:\Stuff\boost_1_70_0, then within that folder will be the hub of the boost include set, c:\Stuff\boost_1_70_0\boost, and it contains all of the boost headers.
boost is referenced by amending the include path to provide access to the boost include hub; not to provide access to the top-most headers in the hub. Similar to openssl, boost prefaces all of their header includes in their own headers, with boost/. The consumers of boost should do the same, Therefore, the include path must include the folder where the boost/ hub can be found. It should not include the boost/ hub itself as part of the path.
Ex: This is correct
g++ -Ic:\Stuff\boost_1_70_0 -o main main.cpp
This, on the other hand is wrong:
g++ -Ic:\Stuff\boost_1_70_0\boost -o main main.cpp
With the former, when code includes:
#include <boost/asio.hpp>
the include path is searched, and the file is found. Further, within that header, when the compiler see this:
#include <boost/asio/associated_allocator.hpp>
it can still resolve correctly, because dropping that "thing" on the end of one of the folders in the include path works.
Now, consider the wrong case. What happens if you configure the include path to accidentally specify the boost/root hub itself? Well, now you can do this:
#include <asio.hpp>
But as soon as the preprocessor starts in on that header it will see:
#include <boost/asio/associated_allocator.hpp>
Um.. woops. The pre-processor will look for this and never find it
Summary
When using boost headers in your source, you always refer to them with the boost hub preamble:
#include <boost/headername.hpp>
and always include the folder where the boost/ hub is located in your build configuration as an amended include path; not full path including the boost/ hub.

Cannot Compile C Program That Uses a Library (FFmpeg) with GCC Because of the Library's Include Statements

I am unable to compile a C project that uses a library called "FFmpeg" with a compiler called "GCC", and I believe it might be either because I don't quite understand how #include works or because I am using the wrong compilation process.
In a folder called Test, I have a file Test/test.c with the following contents:
#include <stdio.h>
#include <stdlib.h>
#include "FFmpeg/libavcodec/avcodec.h"
The folder FFmpeg is located at Test/FFmpeg. When I try to compile this with GCC, I receive the following error:
fatal error: libavutil/samplefmt.h: No such file or directory
The file Test/FFmpeg/libavcodec/avcodec.h has the following code in it:
#include "libavutil/samplefmt.h"
#include "libavutil/attributes.h"
#include "libavutil/avutil.h"
... //many more #include statements
Is the issue here that I need to add "FFmpeg/" to all of these include statements?
If so, is there a way to automatically do this? This library is enormous and probably has hundreds of these statements.
If not, what should I be doing instead? Should I attempt to compile the library by itself? If so, how do I then include this compiled version of the library in my program?
Notes:
The command I am using to compile is gcc -c test.c.
I have GCC installed via MinGW.
I ultimately need to be able to compile this program to both a .dll and an .so.
I apologize if any of the terminology I use here is incorrect or if my explanations are poor. I know almost nothing about compilation. Please let me know if I need to fill in more information.
When #include is used with quotation marks (e.g. #include "file path here"), it will read that file path as a relative file path.
In the case of compiling a C program using GCC, file paths are relative to the current directory. The "current directory" is the one into which you have placed your command prompt using the cd command.
In my case, I cd'd into C:/Users/User/Documents/Test, meaning that all relative file paths are relative to C:/Users/User/Documents/Test. So when my compiler read
#include "libavutil/samplefmt.h"
it basically tried to do this:
#include C:/Users/User/Documents/Test/libavutil/samplefmt.h
when I instead needed the compiler to look at …/Test/FFmpeg/libavutil/samplefmt.h.
It turns out that the solution to this is to give the compiler additional locations to which relative paths might be relative. This is done with the -I[file path here] argument when you compile.
In my case, the way I needed to use this idea was to add C:/Users/User/Documents/Test/FFmpeg as a location to which paths might be relative. Thus, I could have taken my compile command:
gcc -c test.c
And inserted this:
gcc -IC:\Users\User\Documents\Test\FFmpeg -c test.c
However, this is actually an extremely clunky solution. There is a much easier way: it turns out that these file paths you provide with the -I argument can be relative to your current directory themselves. In my case, because my current directory in the command prompt was alreadyC:/Users/User/Documents/Test, I could simply remove this portion from the above command, shortening it to this:
gcc -IFFmpeg -c test.c
And this solved my problem.

GLFW/glfw3.h included failed

I just start learning opengl and I started with GLFW library.
And I download the "Windows pre-compiled binaries" from http://www.glfw.org/download.html. Then I unzip my file into C:\GLFW
And now I have a problem when compiling my code. I use mingw in command line, like
gcc main.c -IC:\GLFW\include\GLFW -LC:\GLFW\lib-mingw -lglfw3 -lglfw3dll -lopengl32 -lgdi32
It always show "fatal error: GLFW/glfw3.h: No such file or directory"
but if I change #include <GLFW/glfw3.h> into just #include <glfw3.h> in my code,
it compiles successfully.
But every tutorial shows me the former. Why?
If I put the header file and lib file into mingw's searching path, is there any different?
It is because you include directly to C:\GLFW\include\GLFW, so there is no GLFW folder in this directory. If you want to use #include <GLFW/glfw3.h>, you will want to use this path instead C:\GLFW\include.
To answer to your two questions:
The include path is very relative to you. What do you prefer, #include <GLFW/glfw3.h> or #include <glfw3.h>?
You can put the lib and header in the mingw's path, but you have to remember that you will still need to enter the good path (#include <GLFW/glfw3.h> if you put it in the folder). I would not recommend this method, since I prefer to make an include and lib folder into my project directory. If you want to do more research: http://www.mingw.org/wiki/includepathhowto.

boost program options config.hpp not found, Mac Xcode 8.3

Trying to compile simple example program with boost::program_options. The suggested include directive for the lib is
#include <boost/program_options.hpp>
I noticed the hard path to boost/program_options.hpp (relative to root folder) is:
boost/libs/program_options/include/boost/program_options.hpp.
And the symlinked path from the root folder boost/program_options/ points to hard path:
boost/libs/program_options/include/boost/program_options/
which is one level below the program_options.hpp file.
I assume I should set my header search path in Xcode to
boost/libs/program_options/include/
and not at boost root?
If I do the former, I get no errors in editor, and auto-completion works, but when I go to compile I get error:
fatal error: 'boost/config.hpp' file not found
#include <boost/config.hpp>
Any advice on how to include this? Have used 1/2-dozen other boost libs without problem.
UPDATE
Sorry, I missed that this lib was NOT header-only.
On building with the program_options lib, it's working fine.

Cannot include a header in MinGW

I'm using SublimeText with MinGW on Windows 7 an wanted to include
#include <boost/multiprecision/cpp_int.hpp>
but I get
fatal error: boost/multiprecision/cpp_int.hpp: No such file or directory
#include <boost/multiprecision/cpp_int.hpp>
Couldn't figure out what to do from what I've found here and in Google.
This is the path to the include folder:
C:\MinGW\include\
Should I add something like? (from what I could "understand")
-I C:/MinGW/include/boost
But it doesn't work...
you need to install boost (from boost.org). http://www.boost.org/users/history/version_1_57_0.html
your #include already starts with boost/ so it will look in include/boost.
UPDATE:
"-IC:/MinGW/include"
Oh okay, I figured it out.
So... I have all the necessary libraries here C:\MinGW\include\
But in order to be able to include boost files (C:\MinGW\include\boost) I needed to copy this folder here C:\MinGW\include\c++\4.9.1 which is where MinGW was looking for my files...
So in the end I get C:\MinGW\include\c++\4.9.1\boost with all the necessary libraries inside. Now it works.