gsl undefined symbol: gsl_multifit_nlinear_trs_lmaccel - c++

I'm having some troubles with the GNU Scientific Lbrary (GSL).
After having intalled it, I tested it with the example here and it works when compiling with:
gcc -Wall -c main.cpp ; gcc -L/usr/local/lib main.o -lgsl -lgslcblas -lm -libtemt
Next, I tried to compile that with no change in the source code nor the compiling command. But here is the problem, when I try to run the result, I get :
./a.out: symbol lookup error: ./a.out: undefined symbol: gsl_multifit nlinear_trs_lmaccel
I tried to comment it out, but it's making other issues. I found that variable in the fulle doc, but I can't find where it's defined.
As the first example is working, I think GSL was installed successfully, and as the same variableis used in different codes, I think it's a global variable, defined in th library.
Does anyone have an idea why I can't access it?
Thank's a lot!

Well, after a lot of tears (no, it's a joke) I found a sort of answer:
I copy and paste libgsl.a and libgslcblas.a in my directory, i wrote that makefile:
CC=gcc
CXXFLAGS=-W -Wall -ansi -pedantic
LIBS=libgsl.a libgslcblas.a -lm
main:main.o
$(CC) -o main main.o $(LDFLAGS) $(LIBS)
main.o: main.cpp
$(CC) -o main.o -c main.cpp $(CXXFLAGS)
I'm happy with that answer even if there is 2 weaknesses:
1) I don't know how to use shared library (at this moment)
2) I don't know how to make it by command line
Hope it will help someone

Related

Igraph makevars will not link to static library, i can use data structures but cannot functions while importing igraph c++ library

I am trying to install my C++ igraph library from https://github.com/igraph/igraph to visual studio code using the following method this is my makefile made according to this link.
CXX = g++
CXX_FLAGS = -std=c++17 -O3 -march=native -DNDEBUG
LIB = -Llib
INC = -Iinclude
.PHONY: all
all: a.out
a.out: main.cpp
$(CXX) $(CXX_FLAGS) $(INC) $(LIB) -ligraph -lm -lstdc++ -lgomp -lpthread -o $# main.cpp
.PHONY: clean
clean:
rm a.out
The compiler will always return something like:
g++ -std=c++17 -O3 -march=native -DNDEBUG -Iinclude -Llib -ligraph -lm -lstdc++ -lgomp -lpthread -o a.out main.cpp
/usr/bin/ld: /tmp/ccqJLfvi.o: in function `main':
main.cpp:(.text.startup+0x9): undefined reference to `igraph_rng_default'
/usr/bin/ld: main.cpp:(.text.startup+0x16): undefined reference to `igraph_rng_seed'
collect2: error: ld returned 1 exit status
make: *** [Makefile:12: a.out] Error 1
If i only want to use data structures such as igraph_t graph* it will work, but if i try to call fucntion it will return error and will not generate a.out file. It would be incredablly good if someone would be able to explain why this happens cuz it really got on my nerve right now.
Please follow the instructions in the documentation to set up your package to link to igraph.
Instructions to install igraph: https://igraph.org/c/html/latest/igraph-Installation.html Note that you must both build and install the package. Make a note of the location you used to install it to (the value of CMAKE_INSTALL_PREFIX)
Instructions on compiling your first igraph program: https://igraph.org/c/html/latest/igraph-Tutorial.html Unless you are already comfortable with writing C programs and linking them to external libraries, I strongly recommend that you use CMake to set up your project, as described in the linked tutorial. CMake works the same way on all platforms (Windows/macOS/Linux) and will automatically figure out how to link your program to igraph correctly. When configuring your project, be sure to set CMAKE_PREFIX_PATH to the location where you installed igraph earlier.

applying the DRY principles in makefile

Just taking a use case for this instance. I'm compiling a c++ file, and sometimes, I'd like to compile without debugging symbols i.e. the -g enabled and sometimes I would like to enable it.
So, I thought of just making two targets in which the second target would reassign a make variable(is it possible) and change the compiling options. I wonder if such a behaviour is possible to achieve with makefiles?
Below is some pseudocode demo and the user enters make first#bg into the command line:
gpp = g++ -std=c++17
first: hello.cpp
$(gpp) hello.cpp -o $#
#/* some other recipes, assuming the list is really long*/
first#bg: main.o
gpp = g++ -g -std=c++17
execute_all_recipe_of_first_target_which_is_really_long_to_copy()
main.o: main.cpp
$(gpp) main.cpp -c -o main.o #the value of gpp should'd also changed here since first#bg executed
If it is possible please provide me with the actual syntax for the demonstrated behaviour. Thanks in advance.
You can do something like this:
first#bg: gpp += -g
first#bg: first
Note that it's more idiomatic to define CXX=g++ and CXXFLAGS=-std=c++17 and then tweak CXXFLAGS, and use make DEBUG=1 for debug builds:
CXX=g++
CXXFLAGS=-std=c++17
ifeq ($(DEBUG), 1)
CXXFLAGS+=-g
endif
Then invoke the compiler as $(CXX) $(CXXFLAGS) hello.cpp -o $# for example. See also this link

C++ file compiling: -L and -I arguments don't work for boost library

There are similar questions but their answers did not work for my issue.
I have a c++ program with #include <boost/test/unit_test.hpp> on top (among other includes).
To compile correctly, if I understood, I should do the command:
g++ -g -L/path_to_boost_lib -lboost_lib myprog.cpp -o myprog.exe
If i do a locate, I get /usr/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.
Hence I edited my call to g++ by doing:
g++ -g -L/usr/lib/x86_64-linux-gnu -lboost_unit_test_framework myprog.cpp -o myprog.exe
But I still get errors of the type undefined reference to boost::unit_test.
I also tried the option -I/usr/include/ which contains the boost folder, without success.
It's because of the order. The GCC linker goes through the artifacts left-to-right, and every unknown symbol it encounters in an object file must be resolved by an artifact occurring afterwards.
The right command is thus:
g++ -g myprog.cpp -L/usr/lib/x86_64-linux-gnu -lboost_unit_test_framework -o myprog.exe
See this answer for a more thorough explanation.
I suggest using a build tool like CMake that takes care of such low-level details for you.

C++ / mysql Connector - undefined reference to get_driver_instance - already tried the easy stuff

Yes this question has been asked before ... I've tried everything mentioned in the previous answers. My setup is really straightforward so this shouldn't be so hard.
I just want to program against mysql using C++. My source code is taken verbatem from the 'hello world' type example here:
http://dev.mysql.com/doc/refman/5.1/en/connector-cpp-examples-complete-example-1.html
I am on Ubuntu 12.10. I am trying:
g++ -Wall -o firsttry_prog -I/usr/include/mysqlcppconn -I/usr/local/boost_1_53_0 -L/usr/lib/x86_64-linux-gnu -l:libmysqlclient_r.so.18 -L/usr/lib/mysqlcppconn -lmysqlcppconn firsttry.cpp
It compiles (if I use -c option) but won't build, giving me the infamous:
/tmp/ccn768hj.o: In function `main':
firsttry.cpp:(.text+0x3a): undefined reference to `get_driver_instance'
A few details:
'firsttry.cpp' is just what I named the source code file, again taken verbatem from the official example
As you can see I AM linking in the mysqlclient library and the mysqlcppconn library. Many times when this question has been asked previously, the answer was to link those.
Some other historical answers suggest the sample source code is wrong and that the function in question needs to be in the sql::mysql namespace etc. I am pretty sure the source code is fine. Again, it compiles, and changing the namespaces in the source code just seems to make it worse.
Thank you in advance for any help you can provide.
So I have now had this problem for a week now and I became very frustrated with it as well. I just now was able to finally build a program that does nothing except login to mysql and I literally squealed with joy. Here is what I have and I hope it helps.
I first compiled the c++ connector library from source but after a while I thought maybe I did something wrong so I then just used apt to get it with:
sudo apt-get install libmysqlcppconn-dev
And here is my simple tester source file "tester.cpp"
#include <stdlib.h>
#include <iostream>
#include <mysql_connection.h>
#include <driver.h>
#include <exception.h>
#include <resultset.h>
#include <statement.h>
using namespace sql;
int main(void){
sql::Driver *driver;
sql::Connection *con;
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306","root","YOURPASSWORD");
return 0;
}
And finally g++ compile command:
g++ -Wall -I/usr/include/cppconn -o testapp tester.cpp -L/usr/lib -lmysqlcppconn
This worked for me and I hope it helps you solve your problem!
For me simply swapping the order of the last two arguments fixed this problem. I don't know why but the linker is able to find the function get_driver_instance if I specify the -lmysqlcppconn option at the end after the source file.
g++ -Wall -o firsttry_prog -I/usr/include/mysqlcppconn -L/usr/lib/mysqlcppconn firsttry.cpp -lmysqlcppconn
Also note that I took out the following options as I think they are redundant
-I/usr/local/boost_1_53_0 -L/usr/lib/x86_64-linux-gnu -l:libmysqlclient_r.so.18
In case you are as forgetful as me and didn't link the library in CMakeLists.txt:
target_link_libraries(<target> mysqlcppconn)
If all the paths are included throw param -I. You would see whether there is a problem if you compile like this:
g++ -g -o0 -I/usr/local/include -I/usr/local/boost/include -c main.cpp -o main.o
g++ -g -o0 -L/usr/local/lib -L/usr/local/mysql/lib -lmysqlcppconn main.o -o test
the problem will appear:
main.o: In function `main':
/home/huangxw/workspace/public/soal/test/main.cpp:165: undefined reference to `get_driver_instance'
collect2: ld returned 1 exit status
Now you must adjust the order of -lmysqlcppconn and main.o:
g++ -g -o0 -I/usr/local/include -I/usr/local/boost/include -c main.cpp -o main.o
g++ -g -o0 -L/usr/local/lib -L/usr/local/mysql/lib main.o -o test -lmysqlcppconn
That is all!!
The reason is simple. You can find out using the web or ask me to elaborate.

Error when run LD_PRELOAD with boost

I compiled LD_PRELOAD which uses boost (locks.hpp). Compile was successfull. I copied this LD_PRELOAD to other linux server, and when i run, error:
/usr/bin/java: symbol lookup error: /test/test.so: undefined symbol:
_ZN5boost11this_thread20disable_interruptionC1Ev
How can i fix this? Can i avoid this problem without installing boost on this server?
How i compile LD_PRELOAD:
g++ -fPIC -m32 -shared -Wl,-soname,test.so -ldl -o test.so test.cpp
Thanks!
It seems you have to get libboost_thread into your test.so file. Something along the lines of:
g++ -fPIC -m32 -shared -Wl,-soname,test.so -ldl -o test.so test.cpp \
/usr/lib/libboost_thread.a -lpthread
Since I wouldn't know the specifics for your system, the boost library might be in a different place than from mine.