g++ linking issue with GSL - c++

g++ -o program main.cpp classOne.cpp classTwo.cpp -lgsl -lgslblas -lm
that's how i compile when the GSL-packages are installed.
but now I'm working on a server where i don't have rights to install GSL-Library.
What are my options?
thx

I had to do this regularly, do as following :
On the server, create one directory in your home directory to install the library (let's say mypref) and another one to build the library (let's say tmp). You have two new directories : ~/mypref and ~/tmp.
Download GSL sources in ~/tmp (last version is ftp://ftp.gnu.org/gnu/gsl/gsl-1.14.tar.gz), extract and go in the generated sub-directory (gsl-1.14) :
cd ~/tmp
wget ftp://ftp.gnu.org/gnu/gsl/gsl-1.14.tar.gz
tar -xvzf gsl-1.14.tar.gz
cd gsl-1.14
Launch the configure script specifying ~/mypref as the installation prefix (and maybe other options depending of your server) :
./configure --prefix=${HOME}/mypref
Make :
make
And install :
make install
Now you can remove safely the ~/tmp directory :
cd; rm -rf tmp
Now you can compile your program using :
g++ -o program main.cpp classOne.cpp classTwo.cpp -I${HOME}/mypref/include -lm -L${HOME}/mypref/lib -lgsl -lgslcblas
-I and -L indicate respectively the path for the headers and the library. If your program is meant to be executed in a context where your home directory is not visible, consider static linking :
g++ -o program main.cpp classOne.cpp classTwo.cpp ${HOME}/mypref/lib/libgsl.a ${HOME}/mypref/lib/libgslcblas.a -I${HOME}/mypref/include -lm
The binary produced by the last command is bigger than previously, but entirely independent from GSL and GSLCBLAS.

Related

Makefile to compile from a specific toolchain

I have receive a makefile from another source. I am supposed to cross-compile on my machine (x86_64, GCC 5.3) it to use on an arm-embedded device (host). I tried to compile with a tool chain (arm_64, GCC7.5) which i installed in my /opt directory by making the following changes (those that are remarked out #) in the makefile of the project
#SYSROOT?=$(shell $(CXX) --print-sysroot)
SYSROOT= /opt/yocto/sysroots/
CXXFLAGS= -std=c++11 -I src/
CCFLAGS= -std=c++11 -I src/
#CXX_LDFLAGS= -L /usr/local/aarch64-linux/lib/ -lpthread -pthread
CXX_LDFLAGS= -L $(SYSROOT)/aarch64-yocto-linux/lib -lpthread -pthread
all: foo
foo: src/foo.cpp
$(CXX) -std=c++11 -g -o $# $^ $(CXX_LDFLAGS)
clean:
rm -rf .objs
However when the code was compiled, it could not be run on host device(arm) and there was warning during make that say along lines of skipping incompatible binaries.
I perform file on the compiled application. It read
ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=99376bcc919a48224e063ec6c6892e1979b5d94e, not stripped
It is clear to me that I have compiled it with my native compiler,libraries etc.
What should i do in this case:
must i do the following before compilation or making the folder
./configure -build xxxxx -host yyyyyy
there is some files, called environment files (full of export settings) in the tool chain directory that seems to be like some configuration settings being exported. Should i use this file? or how should i use them
Regards

How to compile custom cpp files on Google Colab

I'm trying to replicate the result of this github repo using Google Colab since I don't want to install all the requirements on my local machine and to take advantage of the GPU on Google Colab
However, one of the things I need to do (as indicated in the repo's README) is to first compile a cpp makefile. The instruction of the makefile is included below. Obvious I can't follow this instruction since I don't know Google Colab's directories of ncvv, cudalib and tensorflow library
cd latent_3d_points/external
with your editor modify the first three lines of the makefile to point to
your nvcc, cudalib and tensorflow library.
make
Is there a way for me to compile the files included in the makefile (because those functions are needed to run the model) either using the makefile directly or compile each cpp file individually? I included the content of the makefile below to avoid having you to click around in the repo looking for it
nvcc = /usr/local/cuda-8.0/bin/nvcc
cudalib = /usr/local/cuda-8.0/lib64
tensorflow = /orions4-zfs/projects/optas/Virt_Env/tf_1.3/lib/python2.7/site-packages/tensorflow/include
all: tf_approxmatch_so.so tf_approxmatch_g.cu.o tf_nndistance_so.so tf_nndistance_g.cu.o
tf_approxmatch_so.so: tf_approxmatch_g.cu.o tf_approxmatch.cpp
g++ -std=c++11 tf_approxmatch.cpp tf_approxmatch_g.cu.o -o tf_approxmatch_so.so -shared -fPIC -I $(tensorflow) -lcudart -L $(cudalib) -O2 -D_GLIBCXX_USE_CXX11_ABI=0
tf_approxmatch_g.cu.o: tf_approxmatch_g.cu
$(nvcc) -D_GLIBCXX_USE_CXX11_ABI=0 -std=c++11 -c -o tf_approxmatch_g.cu.o tf_approxmatch_g.cu -I $(tensorflow) -DGOOGLE_CUDA=1 -x cu -Xcompiler -fPIC -O2
tf_nndistance_so.so: tf_nndistance_g.cu.o tf_nndistance.cpp
g++ -std=c++11 tf_nndistance.cpp tf_nndistance_g.cu.o -o tf_nndistance_so.so -shared -fPIC -I $(tensorflow) -lcudart -L $(cudalib) -O2 -D_GLIBCXX_USE_CXX11_ABI=0
tf_nndistance_g.cu.o: tf_nndistance_g.cu
$(nvcc) -D_GLIBCXX_USE_CXX11_ABI=0 -std=c++11 -c -o tf_nndistance_g.cu.o tf_nndistance_g.cu -I $(tensorflow) -DGOOGLE_CUDA=1 -x cu -Xcompiler -fPIC -O2
clean:
rm tf_approxmatch_so.so
rm tf_nndistance_so.so
rm *.cu.o
You can use the bash like on your pc by adding %%bash in the colab's cells.
Example:
Cell one: write cpp file
%%writefile welcome.cpp
#include <iostream>
int main()
{
std::cout << "Welcome To AI with Ashok's Blog\n";
return 0;
}
Cell two: compile and run
%%bash
g++ welcome.cpp -o welcome
./welcome
You can also open the cpp file in colab's build-in text editor in order to enjoy correct highlights. It opens when you open a text file from the "Files" tab on the left and can be save with "ctr+s" shortcut.
You can install the required version of Cuda in google colab. For eg.
For Cuda 9.2 you can try
!apt-get --purge remove cuda nvidia* libnvidia-*
!dpkg -l | grep cuda- | awk '{print $2}' | xargs -n1 dpkg --purge
!apt-get remove cuda-*
!apt autoremove
!apt-get update
!wget https://developer.nvidia.com/compute/cuda/9.2/Prod/local_installers/cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64 -O cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64.deb
!dpkg -i cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64.deb
!apt-key add /var/cuda-repo-9-2-local/7fa2af80.pub
!apt-get update
!apt-get install cuda-9.2
Similarly, you can find a way to install Cuda 8.2.
For gcc
!apt-get install -qq gcc-5 g++-5 -y
!ln -s /usr/bin/gcc-5
!ln -s /usr/bin/g++-5
!sudo apt-get update
!sudo apt-get upgrade
Then you can compile it or make it by running make, if your installation has a custom make file.
!make

Use Boost Library Binary in Codelite

I'm new to boost and want to use it with Codelite.
I followed this guide here: Official Boost Guide
Step 1) I installed it to /usr/local/boost_1_67_0/
Step 4) I can compile the example and it works as expected
Step 5) I just ran ./bootstrap.sh and installed everything.
It succeeded and told me
>The Boost C++ Libraries were successfully built!
>
>The following directory should be added to compiler include paths:
>
>/usr/local/boost_1_67_0
>
>The following directory should be added to linker library paths:
>
>/usr/local/boost_1_67_0/stage/lib
So this is what I did in codelite:
6) The example compiles errorlessly with codelite.
>g++ -c "~/Dropbox/MasterThesis/C++/DiracTraceEvaluator/FeynCalc--/main.cpp" -g -O0 -Wall --std=c++11 -o ./Debug/main.cpp.o -I. -I. -I/usr/local/boost_1_67_0/
g++ -o ./Debug/FeynCalc-- #"FeynCalc--.txt" -L. -L./ -L/usr/local/boost_1_67_0/ -L/usr/local/boost_1_67_0/stage/lib/ -lboost_program_options -lboost_regex
Now if I execute the program, I get:
./FeynCalc--: error while loading shared libraries: libboost_regex.so.1.67.0: cannot open shared object file: No such file or directory
I entered /usr/local/boost_1_67_0/stage/lib and all the required files are there. I don't know what else to do.
What solve the issue was
creating a new file in /etc/ld.so.conf.d/
I named it boost.conf
There I entered
# boost_1_67_0 default configuration
/usr/local/boost_1_67_0/stage/lib
Then I ran ldconfig in terminal.

linking pdfium on linux

I'm trying to use the pdfium libraries in linux(debian 64-bit). I managed (finally) to compile the release _x64 version of pdfium and the test programs seem to work. However, I can't seem to to use the libraries in a separate project.
This is my file:
#include <iostream>
#include "fpdfview.h"
#include "fpdftext.h"
#include "fpdfdoc.h"
#include "fpdfedit.h"
main(){
FPDF_InitLibrary();
std::cout << "Hello World!"<<std::endl;
return 0;
}
And this is my Makefile:
CC = g++
CFLAGS = -Wall -g -Wno-unused-variable -Wno-reorder -I/usr/include/pdfium/core/include -I/usr/include/pdfium/fpdfsdk/include -I/usr/include/pdfium/third_party -I/usr/include/pdfium/v8/include
LIBS_pdfium = -static -L/usr/lib/pdfium
LDFLAGS = $(LIBS_pdfium)
Main : Main.o
${CC} ${CFLAGS} Main.o ${LDFLAGS} -o Main
Main.o : Main.cpp
${CC} ${CFLAGS} -c -std=c++11 Main.cpp
clean:
rm *o Main
When I run the makefile the result is:
g++ -Wall -g -Wno-unused-variable -Wno-reorder -I/usr/include/pdfium/core/include -I/usr/include/pdfium/fpdfsdk/include -I/usr/include/pdfium/third_party -I/usr/include/pdfium/v8/include Main.o -static -L/usr/lib/pdfium -o Main
Main.cpp:11: error: undefined reference to 'FPDF_InitLibrary'
collect2: error: ld returned 1 exit status
I've also tried to use the libraries in /home/username/pdfium/out/Release_x64/obj but got the same error
I know that error: undefined reference to FPDF_InitLibrary means that there is a linking error. Therefore I checked the libraries in /home/username/pdfium/out/Release_x64/obj using objdump and one of them contained the InitLibrary symbol. This doesn't seem to make sense...
I don't know if I am referring to the wrong paths in the include or the libraries or if it is something else that is wrong.
I tried to understand the chromiums pdf plugin project makefile since I thought that might help me understand what I am supposed to use but unfortunately it didn't help.
Any ideas for what I am doing wrong?
To compile with PDFium the link line will depend on if you've compiled V8 and/or XFA into your PDFium binary.
With neither of those things enabled you'll need something similar to:
PDF_LIBS="-lpdfium -lfpdfapi -lfxge -lfpdfdoc -lfxcrt -lfx_agg \
-lfxcodec -lfx_lpng -lfx_libopenjpeg -lfx_lcms2 -lfx_freetype -ljpeg \
-lfx_zlib -lfdrm -lpdfwindow -lbigint -lformfiller -ljavascript \
-lfxedit"
PDF_DIR=<path/to/pdfium>
clang -I $PDF_DIR/public -o foo foo.c -L $PDF_DIR/out/Debug -lstdc++ -framework AppKit $PDF_LIBS
public/ is the only directory you should use when working with PDFium for headers. The -framework AppKit is needed on OSX. The PDFium headers are in plain C but you need -lstdc++ as PDFium uses C++ internally and it needs to be able to link in new/delete.
If you're working with V8 you'll need to add in:
-lv8_libbase -lv8_libplatform -lv8_snapshot -licui18n -licuuc -licudata
and if you're using XFA you'll need the V8 includes plus:
-lfpdfxfa -lxfa -lfx_tiff
EDIT
There was recently a pdf_is_complete_lib option added to the PDFium build. Setting that to true in your gn args will create a single libpdfium that can be linked agains. Note, this has only been tested with V8 and XFA disabled.
Args file..
# Build arguments go here.
# See "gn args <out_dir> --list" for available build arguments.
is_debug = false
pdf_is_standalone = true
pdf_use_skia = false
pdf_use_skia_paths = false
pdf_enable_xfa = false
pdf_enable_v8 = false
is_component_build = false
clang_use_chrome_plugins = false
pdf_is_complete_lib = true
use_custom_libcxx = false
Then gn gen your/dir/catalog.
Then ninja -C your/dir/catalog pdfium_all and you take pdfium.a
In Linker
...
g++ -L-I/usr/include/glib-2.0 -o bin/debug/pdfium_test obj/debug/main.o
...
you must have
-pg -s -Wl,--start-group /home/a/repo/pdfium/out/release/obj/libpdfium.a -Wl,--end-group -lpthread -ldl -lpthread
Linking is ok.
I haven't personally built it - because it was too time taking. But I managed to make it work with my golang application using cgo. I used ubuntu 16.04 as my base image in docker. This depends on https://github.com/bblanchon/pdfium-binaries
Following dockerfile downloads the pdfium binary and links to the app you are developing using pkg-config.
FROM ubuntu:16.04
# Specify pdfium version
ARG PdfiumVersion=4026
# Install pkg-config, etc.
RUN apt-get -yqq update && apt-get clean && apt-get install -yqq apt-utils pkg-config tzdata && dpkg-reconfigure -f noninteractive tzdata
# Create .pc file for pkg-config
RUN echo "\n" \
"prefix=/home\n" \
"Name: pdfium\n" \
"Description: pdfium\n" \
"Version: $PdfiumVersion\n" \
"Requires:\n" \
"Libs: -L/home/lib -lpdfium\n" \
"Cflags: -I/home/include\n" > /home/pdfium.pc
# Download and extract pdfium binary
RUN cd /home && wget --quiet https://github.com/bblanchon/pdfium-binaries/releases/download/chromium%2F$PdfiumVersion/pdfium-linux.tgz \
&& tar -xf pdfium-linux.tgz && rm pdfium-linux.tgz
# Setting up paths for pkg-config
ENV LD_LIBRARY_PATH=/home/lib
ENV PKG_CONFIG_PATH=/home/
## COPY YOUR APP TO /app/src/yourApp
# BUILD YOUR APP
WORKDIR /app/src/yourApp
# RUN your app which is linked to pdfium
ENTRYPOINT [“./yourApp"]

Compiling works, starting not - "standard path"?

I want to use SFML with C++ under Ubuntu OS. I create two debug/release shared Libarys with cmake (by this tutorial)
I can compile and link my test application without giving any information about a path. So I think everything is alright with the standard path
g++ -c main.cpp
g++ main.o -o sfml-app -lsfml-graphics -lsfml-window -lsfml-system
but when I start now my application with ./sfml-app it said
./sfml-app: error while loading shared libraries:
libsfml-graphics.so.2: cannot open shared object file: No such file or
directory
this confusing me. Because I think /usr/local/lib is the standard path and when I add this path during compiling
g++ main.o -o sfml-app -L /usr/local/lib -lsfml-graphics
-lsfml-window -lsfml-system
it works. But it should work also without giving information with -L
So what could be the Problem? I have made this before reinstalling Ubuntu. And on my old system it works well, can start my application by ./ terminal and also double clicking.
when I add in the console
export LD_LIBRARY_PATH=$PATH:/usr/local/lib
I can start the application from the console. but only in the specific one. I want the application starts always also by double click (not only from terminal). How can I add the Libary "systemwide" ?
You need to add /usr/local/lib to a file in /etc/ld.so.conf.d/ and then call ldconfig to rebuild its cache.
One way of doing it:
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/usrlocal'
sudo ldconfig
Then you should be able to compile without -L /usr/local/lib nor use export LD_LIBRARY_PATH=$PATH:/usr/local/lib.