Point Cloud Library, linking libraries with g++ - c++

I want to run sample code from PCL site to simple read cloud from file.
I installed libraries, and prepared build script:
g++ -std=c++11 main.cpp -lboost_system `pkg-config --libs --cflags opencv4` -g -o main
but compiller throws many errors like:
/usr/include/pcl/io/pcd_io.h:56: undefined reference to `vtable for pcl::PCDReader'
I know that I need to linking libpcl_io.so like I linked boost library by adding -lboost_system , but my question is - how?
I tried add to script something like -lpcl, -libpcl, -lpcl_io, but it doesn't works
So, what is keyword to linking PCL Libraries?

Related

Xcode linker flags for GSL

I am trying to use the GSL library with C++ in Xcode and I was having an issue linking to the library.
I am able to link to GSL using the terminal and clang with the following linker flags
clang++ -std=c++17 -Wall -pedantic test.cpp -o test -lgsl -lgslcblas
From the command line I'm able to verify it uses the GSL library for the computation. However in Xcode, I get a "'gsl/gsl_linalg.h' file not found" error when I try to build it. Its called in my header file with
#include <gsl/gsl_linalg.h>
I added -lgsl -lgslcblas into Xcode build settings thru the "Other Linker flags" option.
I also ran
gsl-config --cflags
gsl-config --libs
and got the following output
-I/usr/local/include
-L/usr/local/lib -lgsl -lgslcblas
So it seems like GSL is installed properly and can be used by clang but for some reason when I use clang through Xcode it isn't able to use it. I was also able to manually find that particular header file in /usr/local/include/gsl/gsl_linalg.h
Any ideas on what I'm might be doing wrong?

How to properly link a static library in C++ with gcc [duplicate]

I am trying to link GLFW to my C program.
The docs seem to suggest #include<GLFW/glfw3.h> however I have installed 2.7.2 (from my distro's repository) and don't have that header file:
find / -name *glfw* 2> /dev/null
/usr/lib/libglfw.so.2.6
/usr/lib/libglfw.a
/usr/lib/libglfw.so
/usr/lib/pkgconfig/libglfw.pc
/usr/lib/libglfw.so.2
/usr/include/GL/glfw.h
/usr/share/doc/libglfw-dev
/usr/share/doc/libglfw2
/var/cache/apt/archives/libglfw2_2.7.2-1_i386.deb
/var/cache/apt/archives/libglfw-dev_2.7.2-1_i386.deb
/var/lib/dpkg/info/libglfw2.list
/var/lib/dpkg/info/libglfw2.postinst
/var/lib/dpkg/info/libglfw-dev.md5sums
/var/lib/dpkg/info/libglfw2.postrm
/var/lib/dpkg/info/libglfw2.md5sums
/var/lib/dpkg/info/libglfw2.shlibs
/var/lib/dpkg/info/libglfw-dev.list
I tried #include<GL/glfw.h> but I still get undefined reference to 'glfwLoadTexture2D'
How do I link to GLFW and use glfwLoadTexture2D()?
An #include does nothing for the linker; it just brings in declarations, not the actual functions.
The documentation indicates that GLFW uses pkg-config (not surprising; #elmindreda knows her stuff), so your compilation line should be something like:
$ cc `pkg-config --cflags glfw3` -o foo foo.c `pkg-config --static --libs glfw3`
Also note that since the library uses pkg-config, you're not supposed to "care" about details such as where the header and library files are located on your particular installation. Just ask using the --cflags and --libs modes, and you will get the proper locations returned, as the example above indicates.
You are mixing up compilation and linking. If you were missing headers, you would probably have errors a lot sooner than the linking stage.
"Undefined reference" results from symbols not being found by the linker. The most likely cause is you not telling gcc that it should link to the GLFW libraries:
gcc myfile.c -lglfw
When I am on Linux, I compile opengl/glfw projects like this:
gcc main.c -lGL -lglfw
When I am on windows, I compile them by writing:
gcc main.c libglfw3.a -lopengl32 -lgdi32
and I put libglfw3.a file in the same directory where main.c is. I have read people say that they couldn't link properly before writing
-lopengl32 -lgdi32 -luser32 -lkernel32 -lws2_32.
Another thing which may be worth mentioning is that I couldn't link glfw libraries when I downloaded 32bit glfw binaries. When I downloaded 64bit glfw binaries everything worked fine. I have a 64 bit machine and a x86_64-w64-mingw32. I have read comments from people with the opposite experience, where they weren't able to link glfw libraries when they downloaded 64bit binaries, but they were able to link them after downloading 32bit binaries. My advice would be to try both.

Casacore linking error in C++ on Ubuntu 18.04

I installed casacore from source using the GitHub repository on my Ubuntu 18.04. The installation completes without any errors and the respective files are written to the expected directories (the .h files to /usr/local/include & libraries to /usr/local/lib). On trying to compile a basic C++ file using these I'm given the following error:
tmp/ccBxZcf3.o: In function 'main': /home/zealouspriest/C_C++_Projects/bb++/ms.cpp:15: undefined reference to 'casacore::MeasurementSet::MeasurementSet()'
/home/zealouspriest/C_C++_Projects/bb++/ms.cpp:15: undefined reference to 'casacore::MeasurementSet::~MeasurementSet()'
collect2: error: ld returned 1 exit status
The compiler command that I use is as follows:
g++ -g -Wall -I/usr/local/include -L/usr/local/lib -lcasa_casa -lcasa_tables -lcasa_ms ms.cpp -o ms
The ms.cpp file being compiled is extremely simple and just creates an empty measurement set to test for successful linking and is as follows:
//ms.cpp
#include <iostream>
#include </usr/local/include/casacore/ms/MeasurementSets/MeasurementSet.h>
int main(){
casacore::MeasurementSet ms = casacore::MeasurementSet();
return 0;
}
Here is all that I have tried:
a) Building from source using GitHub instructions,
b) Installing from Ubuntu repository.
Thanks in advance for your time!
When compiling manually with g++ you need to first specify your sources, and then the dependencies (libraries):
g++ -o ms ms.cpp -I/usr/local/include -L/usr/local/lib -lcasa_casa -lcasa_tables -lcasa_ms -g -Wall
Better just use CMake if you plan to have something more that just one cpp.
Related topics:
linking files in g++
gcc-g++-parameter-order
Alternatively, you can use the -Wl,--no-as-needed options:
g++ -g -Wall -I/usr/local/include -L/usr/local/lib -Wl,--no-as-needed -lcasa_ms ms.cpp -o ms

Can't link GLFW library with it's header file on Ubuntu 18.04

I've installed the libglfw3-dev:amd64 package on Ubuntu using the standard sudo apt get etc. My following compiling line is:
g++ -o output -IL/usr/lib/x86_64-linux-gnu -lglfw driver.o
My current c++ file is:
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
if (!glfwInit())
return -1;
}
I've tried using local libraries of glfw and setting the -I and -L locations but nothing has seemed to work. I've made sure the .so and .h files are in their respective locations but I always get this error while running make:
g++ -o output -I/usr/include/GLFW -L/usr/lib/x86_64-linux-gnu -lglfw
driver.o
driver.o: In function `main':
driver.cpp:(.text+0x5): undefined reference to `glfwInit'
collect2: error: ld returned 1 exit status
Makefile:2: recipe for target 'output' failed
make: *** [output] Error 1
I've tried looking at all the other SO posts and they recommend compiling with tons of extra flags, but the only thing I've been able to draw from them is that something is wrong with my library since VScode detects the .h files. How can I compile this without any errors?
Have you tried swapping the linker arguments around? That is, compile with
g++ -o output driver.o -lglfw
The linker goes through the files from left to right, and it has to know which symbols from libraries you need, before the libraries are processed.
All is perfectly explained in the manual https://www.glfw.org/docs/latest/build_guide.html#build_link_pkgconfig
The key problem is in your -I/usr/include/GLFW and #include <GLFW/glfw3.h> that gives in sum the path /usr/include/GLFW/GLFW/glfw3.h. I suppose this is a wrong path to glfw3.h. compilation was successful because of the system default include path -I/usr/include.
Do not tune compiler flags manually, let pkg-config do
it for you.
A typical compile and link command-line when using the static version of the GLFW library may look like this:
g++ -o output `pkg-config --cflags glfw3` yourprog.c `pkg-config --static --libs glfw3`
If you are using the shared version of the GLFW library, simply omit the --static flag.
g++ -o output `pkg-config --cflags glfw3` yourprog.c `pkg-config --libs glfw3`

Link error with my own C++ library

This is my first time trying to make a simple library. I worked in Ubuntu 12.04 with g++ 4.6.3. Here is the problem:
[[mylib.cpp]]
#include<sqlite3.h>
void Mylib::blahblah() {...}
void Mylib::evenmoreblah() {...}
...
[[mylib.h]]
#include <...>
class Mylib {
...
};
Then I made the lib by:
gcc -c -Wall -fpic mylib.cpp
gcc -shared -o libmylib.so mylib.o
I used the library in a single test.cpp which contains only the main(). I put libmylib.so in ./libdir, and compiled by using:
g++ -g test.cpp -o test -lpthread -L/usr/local/lib -lsqlite3 -L./libdir -lmylib
The error I got:
./libdir/libmylib.so: undefined reference to `sqlite3_close'
./libdir/libmylib.so: undefined reference to `sqlite3_exec'
./libdir/libmylib.so: undefined reference to `sqlite3_free'
./libdir/libmylib.so: undefined reference to `sqlite3_open'
You could link -lsqlite3 into your shared library with
gcc -shared mylib.o -o libmylib.so -lsqlite3
If you do that, you don't need to explicitly link -lsqlite3 to your program, but that won't harm.
and the order of linking arguments for your program is important:
g++ -Wall -g test.cpp -o mytest \
-L./libdir -lmylib -L/usr/local/lib -lsqlite3 -lpthread
it should go from higher-level libraries to lower-level (i.e. system) ones. And don't forget -Wall to get almost all warnings from the compiler, which is very useful.
Read the Program Library HowTo.
PS. Don't call your program test which is a shell builtin (and the standard /usr/bin/test). Use some other name.
If your library make references to sqlite3, you should link sqlite after linking your library :
g++ -g test.cpp -o test -lpthread -L/usr/local/lib -L./libdir -lmylib -lsqlite3
Otherwise ld won't find anything useful in libsqlite3 before linking your library and won't be able to find the requested symbols after that.
Since your library uses sqlite3, you need to add that AFTER your own library in the linker command. I think you could add it to the linking of your shared library too, but not certain.
The linker resolves libraries and their references in the order you list them, so the order is important.