Using CGAL Libraries with Eclipse on Ubuntu 16.04 - c++

I am trying to run some "Hello world" tests with CGAL libraries under Eclipse Oxygen, but I am failing with a dozen of undefined reference errors.
I followed the suggested apt-driven installation method by running
sudo apt-get install libcgal-dev
sudo apt-get install libcgal-demo
But now I am completely lost on how to link the libraries and make Eclipse correctly include and use CGAL.
This is the program that I'm trying to build:
#include <iostream>
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <sstream>
typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
typedef Kernel::Point_2 Point_2;
int main()
{
Point_2 p(0, 0.3), q, r(2, 0.9);
{
q = Point_2(1, 0.6);
std::cout << (CGAL::collinear(p,q,r) ? "collinear\n" : "not
collinear\n");
}
{
std::istringstream input("0 0.3 1 0.6 2 0.9");
input >> p >> q >> r;
std::cout << (CGAL::collinear(p,q,r) ? "collinear\n" : "not
collinear\n");
}
{
q = CGAL::midpoint(p,r);
std::cout << (CGAL::collinear(p,q,r) ? "collinear\n" : "not
collinear\n");
}
return 0;
}
And this is an example of the errors that I'm receiving:
make all
Building target: exact
Invoking: Cross G++ Linker
g++ -L/usr/lib/x86_64-linux-gnu -L/usr/lib -o "exact" ./exact.o
./exact.o: In function `CGAL::Handle::Handle(CGAL::Handle const&)':
/usr/include/CGAL/Handle.h:55: undefined reference to
`CGAL::precondition_fail(char const*, char const*, int, char const*)'
makefile:44: recipe for target 'exact' failed
I tried to include the directories where the GCAL libraries are located in Eclipse under the Cross G++ Linker and Cross GCC compiler, but to no avail.
Unfortunately I am a total beginner both in C++ programming and in Eclipse!
Thanks for the Help!

I have suffered through this multiple times. :D
Here are the lessons I have learned*:
Do not download cgal from the ubuntu repository - download from their website and build from source
Make sure you have all the dependencies (boost, etc) installed
Right-click on your project -> Properties -> C/C++ Build ->Settings ->Tool Settings.
Under GCC C++ Linker:
add libraries CGAL,gmp,mpfr,boost_thread
set library search path: (your path)/CGAL-4.11.1/lib
Under GCC C++ Compiler: -> includes: add include path (yourpath)/CGAL-4.11.1/include/
Try now. If it still does not because you cannot dynamically link the right files, add "-rpath /home/merlin/UbuntuSoftware/CGAL-4.11.1/lib " under miscellaneous under the linker.
Hope that helps.
Merlin :)

Related

Am not able to link a shared library and run executable on mac (Intel tbb)

I was trying to compile and link Intel's tbb into a simple program on my mac, but I keep getting an error saying that the library was linked properly.
dyld: Library not loaded: #rpath/libtbb.dylib
Referenced from:<path>/./a.out
Reason: image not found
Trace/BPT trap: 5
The test program I used has the following code
#include <iostream>
#include <cassert>
#include <tbb/concurrent_queue.h>
int main() {
auto q = tbb::concurrent_queue<int>{};
q.push(1);
auto top_value = int{};
assert(q.try_pop(top_value));
assert(top_value == 1);
assert(q.empty());
return 0;
}
And this is how I built the library (with downloading the library included for your convenience)
curl -OL https://www.threadingbuildingblocks.org/sites/default/files/software_releases/source/tbb2017_20170118oss_src.tgz
tar -xzvf tbb2017_20170118oss_src.tgz
mv tbb2017_20170118oss tbb
rm tbb2017_20170118oss_src.tgz
cd tbb
make tbb
mv build/*_release build/tbb_release
And I tried to compile the program with this command
g+++ another.cpp -I tbb/include -L tbb/build/tbb_release -ltbb -ldl
And then on running the program I get the error above. Any ideas on how I can go about fixing this?
My compiler's version is clang-800.0.42.1 and the operating system I am running is 10.11.5
Thanks!

undefined reference to `tesseract::TessBaseAPI::TessBaseAPI()'

I am trying to do something with the tesseract c++ library but I get the following linker error(mingw32-g++):
undefined reference to `tesseract::TessBaseAPI::TessBaseAPI()'
I am using Code::Blocks as my IDE and have done the following steps to prepare my environment:
Downloaded the sources and prepared my build folder as descripted at http://tesseract-ocr.googlecode.com/svn/trunk/vs2008/doc/setup.html
Created a new Code::Blocks project
Added all libraries included in the lib directory to the Project (Settings->Compiler Settings->Linker Setting->Link libraries->add)
Added the three additional folders (include, include/tesseract, include/leptonica) to the search directory list (Settings->Compiler Settings->Search directorys->add)
Then I have written some code to test my configuration ...
#include <baseapi.h>
#include <allheaders.h>
#include <iostream>
using namespace std;
int main()
{
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
return 0;
}
... and the above written linker error occurs.
I donĀ“t know what I am doing wrong, hope that anybody have an idea.
Thanks a lot.
You have to add the libraries under the Other Linker Flags in Build Settings.
Try adding -ltesseract and -lstdc++ and the paths to tesseract and leptonica libraries in the following convention:
-L/usr/local/Cellar/tesseract/3.04.01_2/lib
I had to add all the C/C++ include paths:
..\tesseract_3.05\api
..\tesseract_3.05\ccmain
..\tesseract_3.05\ccutil
..\tesseract_3.05\ccstruct
..\tesseract_3.05\classify
..\tesseract_3.05\cube
..\tesseract_3.05\cutil
..\tesseract_3.05\dict
..\tesseract_3.05\neural_networks\runtime
..\tesseract_3.05\textord
..\tesseract_3.05\viewer
..\tesseract_3.05\opencl
..\tesseract_3.05\wordrec
..\leptonica\src
..\liblept
Also I added all the libraries:
giflib.lib
libjpeg.lib
liblept.lib
libpng.lib
libtesseract.lib
libtiff.lib
libwebp.lib
openjpeg.lib
zlib.lib
In ubuntu, installing tesseract and leptonica:
sudo apt install tesseract-ocr
sudo apt install libtesseract-dev
(see:https://github.com/tesseract-ocr/tesseract/wiki)
Place the headers:
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
Compile:
g++ program.cpp -o program -llept -ltesseract

undefined reference to glfwSetErrorCallback

I'm trying to use the GLFW library, but am having difficulty with compiling a simple program. I went to the GLFW website and download the latest release, then using "How to build & install GLFW 3 and use it in a Linux project" I built and installed it.
Here's my code:
#include <GLFW/glfw3.h>
#include <iostream>
using std::cout;
using std::endl;
void GLFW_error(int error, const char* description)
{
fputs(description, stderr);
}
void run()
{
cout << "pooch" << endl;
}
int main()
{
glfwSetErrorCallback(GLFW_error);
if (!glfwInit()) exit(EXIT_FAILURE);
run();
glfwTerminate();
return 0;
}
Using the command line:
bulletbill22#ROBOTRON ~/Desktop $ g++ -std=c++11 -lglfw source.cpp
yields
source.cpp:function main: error: undefined reference to 'glfwSetErrorCallback'
glfwSetErrorCallback is taken from their tutorial for "Setting an error callback".
Inclusion of -glfw3 results in /usr/bin/ld: error: cannot find -lglfw3
Even though everything seemed to be installed correctly, I suspect the problem may lie somewhere with the installation of the GLFW library because I'm not used to CMake and don't entirely understand how it works. I'm frustrated because the answer must be simple, but I'm not sure which keywords are really relevant when googling the problem; mostly the results are people who were incorrectly compiling with CMake, which I'm not compiling with in this case.
It seems that the directories for the glfw3.h header and libglfw3.so (and/or libglfw3.a) library are not in the default path.
You can check with by adding the -v option to the g++ options. Locate the directory where the glfw3.h header is found - call this $GLFW_INCDIR - it typically ends with .../GLFW. Locate the directory where the library is found - call this $GLFW_LIBDIR. Try:
g++ -std=c++11 -I$GLFW_INCDIR source.cpp -o pooch -L$GLFW_LIBDIR -lglfw3
If all the library dependencies are satisfied, this hopefully results in a program called pooch.
One other thing: GLFW3 is a C library, and the callback function arguments are expected to be C functions. So your callback should have 'C' linkage, i.e.,
extern "C" void GLFW_error(int error, const char* description) ...
Also, if you're having trouble with cmake, you may have ccmake installed. Try ccmake . in the top-level directory of the GLFW3 package for 'interactive' configuration.

c++ program using GMP library

I have installed GMP using the instruction on this website: http://www.cs.nyu.edu/exact/core/gmp/
Then I looked for an example program using the library:
#include <iostream>
#include <gmpxx.h>
using namespace std;
int main (void) {
mpz_class a, b, c;
a = 1234;
b = "-5678";
c = a+b;
cout << "sum is " << c << "\n";
cout << "absolute value is " << abs(c) << "\n";
cin >> a;
return 0;
}
But if I compile this using the command: g++ test.cpp -o test.exe, it says gmpxx.h: no such file or directory.
How can I fix this? I am kind of new to this. And I am using MinGW.
Get the actual version here GNU GMP Library. Make sure you configure it to be installed in /usr/lib (pass --prefix=/usr to configure).
Here you have documentation: GNU GMP Manual.
You are not using the lib correctly. I don't know if you can directly access mpx values
with C++ functions but, here you have a working example of what you wanted to achieve:
#include<iostream>
#include<gmp.h>
using namespace std;
int main (int argc, char **argv) {
mpz_t a,b,c;
mpz_inits(a,b,c,NULL);
mpz_set_str(a, "1234", 10);
mpz_set_str(b,"-5678", 10); //Decimal base
mpz_add(c,a,b);
cout<<"\nThe exact result is:";
mpz_out_str(stdout, 10, c); //Stream, numerical base, var
cout<<endl;
mpz_abs(c, c);
cout<<"The absolute value result is:";
mpz_out_str(stdout, 10, c);
cout<<endl;
cin.get();
return 0;
}
Compile with:
g++ -lgmp file.cpp -o file
Here is the correct procedure for setting up the current (as of 7/2/13) GNU bignum libraries with Eclipse CDT, MinGW, and msys for C++. To get through this, you should have used Unix or Linux before, as well as Windows, and you should have a vague recollection of programming and compiling programs. This is the culmination of over a week of research and hardcore frustration, so if I messed something up note it politely or I will blow you up with the power of my mind!
I assume you have already downloaded and installed Eclipse and MinGW and have installed msys into MinGW. You must install MinGW before msys!
Download the tarball for the GMP libraries from gmplib.org to ${gmp_download}. I downloaded the gmp-5.1.2.tar.xz because I have never used lzip and didn't know if it was available in msys.
Open up an msys window (essentially a bash shell). cd ${gmp_buid} and tar -Jxvf ${gmp_download}/gmp-x.x.x.tar.xz
Those tar options are different from what you may find elsewhere on the web! -Jxvf is right for xz (and I think lzip), but for gzip you use -xzvf.
cd gmp-x.x.x and run ./config.guess. Write down the output. You will need it next.
Run ./configure --prefix=${gmp_build} --build= --enable-cxx --with-gnu-ld
Apparently if you don't explicitly tell GMP to build for your platform it builds everything, which is bad. The cxx option builds the C++ libraries and --with-gnu-ld allows it to work with ld. Pretty straightforward.
make
make install
EX: suppose you installed to C:/gmp. You should have gmp/include/gmp.h and gmpxx.h. You should also have gmp/lib/libgmp.a, libgmp.la, libgmpxx.a, libgmpxx.la. You should also have a share directory with stuff in it.
Set up eclipse:
Go to project --> properties
Under C/C++ build --> Environment edit the PATH variable and add ${gmp_build}/include;${gmp_build}/lib
Under C/C++ build --> settings --> tool settings --> GCC Assembler --> general add ${gmp_build}/include as an include path.
Same place but --> GCC C++ compiler --> Includes add ${gmp_build}/include as an include path.
Same place --> GCC C++ compiler --> Miscellaneous add -lgmp -lgmpxx to the END of the line. THE END OF THE LINE!
Same place --> GCC C compiler Add the same include paths and miscellaneous options as before.
Same place --> MinGW C++ linker --> Libraries Add to the "Libraries (-l)" both gmp and gmpxx IN THAT ORDER! Now add ${gmp_build}/lib to "LIbrary Search Path (-L)"
Under C/C++ General --> Paths & Symbols --> Incudes Tab check that you have ${gmp_build}/include in your include directories for Assembly, C, and C++. If they aren't there you may have messed up an earlier step. They should be auto populated by Eclipse.
Same place --> Libraries Tab check that you have gmp and gmpxx IN THAT ORDER. It should already be populated.
Same Place --> Library Paths Tab Check for ${gmp_build}/lib which should already be there.
Hit "Apply" and make sure you rebuild the index or the changes won't take. Hit OK to close out.
Run this short program to verify your setup:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <gmp.h>
#include <gmpxx.h>
using namespace std;
int main ()
{
mpz_t p;
mpz_init_set_ui (p,3);
return 0;
}
Your compile commands should look similar to this:
g++ "-IC:\gmp\include" -O0 -g3 -Wall -c -fmessage-length=0 -lgmp -lgmpxx -o main.o "..\main.cpp" g++ "-LC:\gmp\lib" -o GMPDebug.exe main.o -lgmp -lgmpxx
Notes:
The order of the options is important. I don't know all of the whys, but if the second command line (which links the program) has the -lgmp -lgmpxx flags before the -o option, the linking will fail miserably.
The -l flag is a tricky one. It actually says "Go look in -L for liblibrary.a". In this case "Go look in C:\gmp\lib for libgmp.a and libgmpxx.a".
I have heard of bugs involving cout and the 64 bit version of eclipse, so I am using the 32 bit version, where I am seeing the same bug. :-)
Since there are very small examples in gmp library docs, I'm including exponentiation example for reference:
Program calculates 2 ^ 20000
#include <iostream>
#include <gmp.h>
using namespace std;
int main(void) {
mpz_t result, base;
mpz_inits(result,base,NULL);
mpz_set_str(base, "2", 10);
mpz_pow_ui(result, base, 20000);
mpz_out_str(stdout, 10, result);
return 0;
}
Compile:g++ -o gmp_pow_test gmp_pow_test.cpp -lgmp
Run :./gmp_pow_test
Install gmp library on Ubuntu with following: sudo apt-get install libgmp-dev libgmpxx4ldbl
You need to tell the compiler what libraries you want to use.
g++ -lgmp -lgmpxx file.cpp -o file
It is probably too late to be useful, but...
First, your program works just fine. As others pointed out, you need to (a) ensure that GMP library is installed (including its gmpxx extension, and all the relevant files), and (b) that you're telling the compiler where to find both the include files, and the libraries to link with.
In my case include files are in /opt/local/include, and the libraries are in /opt/local/lib (where Macports placed them :).
Here's the code:
#include <iostream>
#include <gmpxx.h>
using namespace std;
int main (void) {
mpz_class a, b, c;
a = 1234;
b = "-5678";
c = a+b;
cout << "sum of " << a << " and " << b << " is " << c << "\n";
cout << "absolute value is " << abs(c) << "\n";
// cin >> a;
return 0;
}
Here's the compile/link command:
clang++ -o gmpxx-tst -I/opt/local/include gmpxx-tst.cpp -L/opt/local/lib -lgmpxx -lgmp
Here's what invocation of gmpxx-tst produces:
$ ./gmpxx-tst
sum of 1234 and -5678 is -4444
absolute value is 4444
$
You'll need to tell the compiler where the header file is.
g++ test.cpp -I/path/to/directory/that/contains/the/header -o test.exe
I have tried so many solutions and all of them has some problems
Here is the best way to install GMP and eclipse
Follow this link
http://www.multigesture.net/articles/how-to-install-mingw-msys-and-eclipse-on-windows/
You need to make sure of the following that hasn't been mentioned there:
When installing MinGW choose a path that contains no space like "c:\MinGW"
Once installed, From start open MinGW installation manger
choose all the basics
-then under MinGW, choose all GMP libraries to be installed
Apply changes
After that you will install JDK, then Add "C:\Program Files\Java\jdk1.8.0_121\bin" to PATH system variable
After installing Eclipse go to:
GCC C++ compiler --> Miscellaneous add -lgmp -lgmpxx to the END of the line
MinGW C++ linker --> Libraries Add to the "Libraries (-l)" both gmp and gmpxx IN THAT ORDER
gooooo ahead.

Linking programs to C++ Project in Eclipse? Linux

I am given the command to link programs:
g++ -o prog_name prog_name.cc -lsicklms-x -pthread
However, I'm not using the command line, I'm using eclipse.
My Project is called "Test" and I have only 1 class "main.cpp" that is this:
/*
* Test.cpp
*
* Created on: Dec 1, 2011
* Author: igvc
*/
#include <iostream>
#include <sicklms-1.0/SickLMS.hh>
using namespace std;
using namespace SickToolbox;
int main(int argc, char *argv[]) {
/* Specify device path and baud */
string dev_path = "/dev/ttyUSB0";
sick_lms_baud_t lms_baud = SickLMS::SICK_BAUD_38400;
/* Define buffers for return values */
unsigned int measurements[SickLMS::SICK_MAX_NUM_MEASUREMENTS] = {0};
unsigned int num_measurements = 0;
/* Instantiate the object */
SickLMS sick_lms(dev_path);
try {
/* Initialize the device */
sick_lms.Initialize(lms_baud);
/* Grab some measurements */
for(unsigned int i = 0; i < 10; i++) {
sick_lms.GetSickScan(measurements,num_measurements);
cout << "\t" << num_measurements << endl;
}
/* Uninitialize the device */
sick_lms.Uninitialize();
}
catch(...) {
cerr << "error" << endl;
return -1;
}
return 0;
}
I'm getting all these errors:
undefined reference to `SickToolbox::SickLMS::~SickLMS()`
undefined reference to `SickToolbox::SickLMS::GetSickScan(unsigned int*, unsigned int&, unsigned int*, unsigned int*, unsigned int*, unsigned int*, unsigned int*)'
undefined reference to `SickToolbox::SickLMS::Initialize(SickToolbox::SickLMS::sick_lms_baud_t)'
undefined reference to `SickToolbox::SickLMS::SickLMS(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
undefined reference to `SickToolbox::SickLMS::Uninitialize()'
So I think it's a linking problem. And I have that above command to link, which the tutorial says is required, but I'm not sure how to set it up in eclipse.
Where the code says #include sicklms-1.0/SickLMS.hh, I did go to Project>Properties>C/C++ Build>Settings>GCC C++ Compiler>Directories and added the directory to the .h file and that resolved that problem, but now I'm getting those errors. I went to GCC C++ Linker>Libraries>Library search path(-L) and added the sicktoolbox-1.0.1 directory, but i don't know what to put in the Libraries(-l) part.
Thanks for any help, I'm really stuck on this.
Presumably you are using Eclipse CDT?
Right-click on your project and select Properties from the context menu.
In the Properties dialog, select C/C++ Build->Settings->GCC C++ Linker->Libraries
In the Libraries box, click on the + button to add a new library, type sicklms
Your library must be in the linker's path. /usr/lib and /usr/local/lib are fairly standard. The library name must be libsicklms.a or libsicklms.so for -l as the linker prepends lib to the library name.
If sicklms was installed from source with ./configure && make && make install then the library should be in the standard library path i.e. either /usr/lib or /usr/local/lib otherwise, your problem is not your linker, but the way sicklms was installed.
If any of the above instructions are not doable, you need to let me know
a) Version of Eclipse (Galileo, Indigo, etc)
b) If you have CDT installed.
c) How sicklms was installed on your system (package manager, built from source, etc).
Based entirely on what you've written here, I think in the -l part you need to just add -lpthread -lsicklms-x. But that's assuming Eclipse provides enough smarts to replace a tool such as make(1), which is really fantastic for rebuilding large projects.
Try creating a Makefile in your project directory like this:
LDFLAGS=-L/path/to/sicklms-directory -lsicklms-x -lpthread
Running make then gives:
$ make main
g++ -L/path/to/sicklms-directory -lsicklms-x -lpthread main.cpp -o main
/usr/bin/ld.bfd.real: cannot find -lsicklms-x
collect2: ld returned 1 exit status
make: *** [main] Error 1
(It fails because I don't have your libraries available. But it does show the command line that was executed to try to build the source.)
You can probably make some easy changes for compiling Java programs too. I've only got super-simplistic Java sources easily available, but the following Makefile lets me re-build them very easily:
$ cat Makefile
.SUFFIXES: .java .class
.java.class:
javac $<
all: Echo.class Test.class Compress.class
$ make Echo.class Test.class
javac Echo.java
make: `Test.class' is up to date.
$ rm *class
$ make
javac Echo.java
javac Test.java
javac Compress.java
(Note that the indented line is indented with a tab. It must be a tab. make(1) is mighty archaic in some seriously inconvenient ways.)