Backslash on Include directories with pkg-config on Mac OS Lion - c++

Good evening everybody,
I have virtually no experience with pkg-config but it seems that I will have to use it in working with openssl. Im on a Mac, which might be of some importance.
The problem is:
i compile my testing program with
cc test.cc `pkg-config --libs --cflags openssl` -o test
i have compiled openssl and installed it in ~/openssl afterwards adding ~/openssl/lib/pkgconfig to the PKG_CONFIG_PATH
since I want to use the newest version instead of the preinstalled one, I specify the options with every compilation
the result of running pkg-config --libs or pkg-config --cflags openssl is -L\~/openssl/lib -lssl -lcrypto
This begs the question for me why there is a backsash and whether my compiled version is used or the preinstalled one and if the first one is the case how i could possibly fix this...
Any kind of help is appreciated

The expansion of ~ to your home directory only occurs in certain circumstances. Basically, it's the shell that's doing it. If a program or a file API sees the tilde, it is interpreted as a literal; it's not expanded.
Use $HOME when setting PKG_CONFIG_PATH instead, as in $HOME/openssl.

Related

Link PCL libraries while compiling C++ programs in Linux

I am new to the Point Cloud Library. There is a thing that has been bugging me for some time.
So, on my system, whenever I have to compile a C++ program, which requires OpenCV libraries to be linked, I use the following terminal command:
g++ -std=c++11 fileName.cpp -o executableFile `pkg-config --cflags --libs opencv`
Now, things have turned to a point where I have to use PCL. But, everywhere (including the PCL's official website) people link PCL libraries using a CMake file, and I am not familiar with CMake.
Is there a way to include the PCL libraries without writing a CMake file and just including some more flags/parameters to the terminal command?
I am using Ubuntu 18.04.
I experimented for a while, and here is how I figured this out.
man pkg-config tells you the folders where pkg-config looks for .pc files. So, I checked those folders for the exact .pc file names that I want pkg-config to link with my .cpp file. I found the required file (pcl_io-1.11.pc) at \usr\local\lib\pkgconfig
Next, I modified my terminal command to the following (please consider two back-quotes as a single back-quote below)
g++ -std=c++14 pcd_write.cpp -o pcd_write ``pkg-config --cflags --libs pcl_io-1.11`` -lboost_system
Note: Not including the -lboost_system would result in another error message. I found this helpful.
This compiled successfully. But, on running the executable, I would get this error message:
./pcd_write:error while loading shared libraries:libpcl_io.so.1.11:cannot open shared object file:No such file or directory
The solution to this problem was found here
sudo /sbin/ldconfig -v
Then, running the executable gave the expected results.

Error loading SDL2 shared libraries while executing program on another pc

I'm trying to compile a program i made using SDL2 to work on others computers (or testing VM in this case).
I've been compiling it with what i think are the correct flags, e.g. g++ main.cpp -o main -lSDL2, however when i try executing it on another Ubuntu installation i get this error.
error while loading shared libraries: libSDL2-2.0.so.0: cannot open shared object file: No such file or directory
From my understanding it's not a problem in my compiling but with how i expect it to work on another Linux installation; I've cross-compiled (using mingw32) and tested it (using a freshly installed VM) on Windows adding the correct dlls with the exe (seems to work fine) and I was expecting for it to work in a similar fashion.
What's the standard in this cases? Should i write a setup scripts to install the library dependencies on the target machine? Is there another way I'm not aware of? I've never released an application for Linux (nor Windows) and I'm struggling to find the resources to do things "the right way".
Thanks for everyone suggestions, I ended up settling for the easy way, compiling the "easy to install" libraries dynamically e.g.-lSDL2 and the others statically (checked the licenses and it should be fine) like so:
g++ main.cpp -o main -Wl,-Bdynamic -lSDL2 -lSDL2_image -lSDL2_ttf -Wl,-Bstatic -lSDL2_gfx -static-libgcc -static-libstdc++
I'll put in my documentation how to install the required SDL2 libraries.
I am not sure how familiar you are with pkg-config, but the output for sdl2 is this:
-D_REENTRANT -I/usr/include/SDL2 -lSDL2
This was found from running this:
pkg-config --cflags --libs sdl2
Basically, you need to point to where SDL2 is located BEFORE you actually link to it.
The tool pkg-config is designed to tell you the information you need when you want to link to a package in Linux. You were linking with the library, but you forgot to tell GCC where the library is located.
If you want to compile you code, try this:
g++ main.cpp -o runme `pkg-config --cflags --libs sdl2`
This will automatically grab all of the flags that you need to compile with SDL2 included.
Oh, and you should note, ORDER MATTERS WHEN ADDING FLAGS AND LIBRARIES!!!
There are many questions on SO where the order of compiler options caused all of the problems. Do not be like those people. I suggest you search SO for more info on that.

How Do I Get WxWidgets setup.h after I install all the .debs on Debian?

Problem first, then details:
I copied a hello-world program from the wxwidgets tutorials and tried to compile it from the command line like this:
g++ -o h wxhello.cpp -I/usr/include/wx-3.0
The compile terminated quickly because it could not find "wx/setup.h". I researched this apparently EXTREMELY COMMON PROBLEM and learned that there is supposed to be a second include path, pointing to the place where the individual setup.h that suits my situation can be found. So I tried:
find /usr/include/wx-3.0 -name "setup.h"
And the output was nothing.
So I installed wxWidgets by marking libwxgtk3.0-dev in Synaptic and allowing all the dependencies to be installed (something like 40 packages in all because I just set this thing up).
How do I get my program to compile?
You need to have wx-config --cxxflags --libs in your command line enclosed in back-ticks like this
g++ -o h wxhello.cpp `wx-config --cxxflags --libs`
This is not the solution on Windows, but it should work on any Linux.
This is not documented in a searchable fashion as far as I can tell.

C++ where are the flags for linking libraries documented?

With every new library I want to use I have noticed there is a specific flag, or set of flags, I have to add to the compile in order for it to actually link the library.
For example with GL GLEW and GLUT I use "-lglut -lGLU -lGL".
The problem is the only way to find these flags that I need is to search endlessly on google for them. Is there a 'standard' place to find these flags for any library? Are they appended to the tops of the headers or something? I hope it's just something obvious I'm missing.
Typically, you develop an intuition for discovering these things on the platforms you develop for. It does suck.
For example, on my Debian development system, I would start by checking pkg-config for the library I want.
pkg-config --list-all | less -i
It looks like there's a package named gl. So, you can get the linker flags using pkg-config:
pkg-config --libs gl
pkg-config --cflags gl
Then you can put that into your Makefile:
gl_cflags := $(shell pkg-config --cflags gl)
gl_libs := $(shell pkg-config --libs gl)
override CFLAGS += $(gl_cflags)
override LIBS += $(gl_libs)
my_app: my_app.o
$(CC) $(LDFLAGS) -o $# $^ (LIBS)
This doesn't work for all libraries, only those with .pc files installed. For example, it won't work for LibJPEG. In these cases, you would either remember the flags (-ljpeg) or you would use an Autoconf / CMake / etc. configuration script to discover the flags at configuration time.
And, none of this will work at all if you don't have the development versions of your libraries installed.
sudo apt-get install libgl1-mesa-dev
P.S. GLU is a bit obsolete.
I'll assume you are asking about GCC/G++ compiler.
The flag is -l<libName>, where <libName> is the name of the library you want to link against. For example, -lGL links against the GL library.
The reason some libraries require multiple flags in certain order is simply the way they depend on each other (the libraries your library depends on might also depend on some other ones, and so on). The library's documentation should solve the problem in most cases.

Linking the Allegro library to a C++ application using the g++ compiler (Ubuntu)

In trying to get Allegro (A C++ game programming library) to work with a very simple C++ application in Ubuntu 12.04, I am unable to get the program to compile with the allegro header definitions. It returns the error allegro.h - no such file or directory found. I tried running a pkg-config to find the proper linker command, but that didn't help in compilation.
I am almost certain it is installed correctly at this point. I tried using a pkg-config --cflags --libs allegro-5.0 for the include file paths, none of which worked when using in the g++ compile line.
Thanks in advance.
Running a pkg-config --cflags --libs allegro-5.0 told me wrong on the include path. It told me to use -I/usr/local/include and after some digging into that folder, I found that the include path is -I/usr/local/include/allegro5 instead. It is compiling fine now.