Undefined references when mixing gfortran and g++ compilation - c++

I am trying to mix Fortran and C++ object files, which are perfectly working and producing executable with a simple C++ file. However, when I am trying to mix the same Fortran object file with the main C++ code (which is a very large and complicated code) it is producing following errors listed below. I have played with the positions of -lgfortran but no luck.
The command used is
g++ -o test *.o -static -lpthread -L/usr/local/lib -lquadmath -lgfortran
and all kind of permutations are also have been tried.
Errors are:
gcc_4_9_2_release/libgfortran/io/write_float.def:1300: undefined reference to `signbitq'
gcc_4_9_2_release/libgfortran/io/write_float.def:1300: undefined reference to `finiteq'
gcc_4_9_2_release/libgfortran/io/write_float.def:1213: undefined reference to `finiteq'
gcc_4_9_2_release/libgfortran/io/write_float.def:1300: undefined reference to `isnanq'

Working command is
g++ -static -o test *.o -lgfortran -lpthread -L/usr/local/lib -lquadmath

'nm' both the c.o and the fortran.o
I am guessing it is probably an underscore you need.

Related

g++ undefined reference to library symbols [duplicate]

This question already has answers here:
Why does the order in which libraries are linked sometimes cause errors in GCC?
(9 answers)
Closed 5 years ago.
There are linker errors to Symbols defined by SFML, but i cannot see how they occur despite that I linked the lib.
I'm using make, which I currently learn and I want to build a minimalistic dev-environment with it.
Give a holler if you need anymore information than the following. I'd just like to minimize the questions size.
XXX#XXX ~/Documents/dev/cpp/proj/beep $ make clean
rm -f build/*.o build/release/*.o build/debug/*.o build/test/*.o
XXX#XXX ~/Documents/dev/cpp/proj/beep $ make tests
//test obj first
g++ -std=c++14 -Wall -pthread -Iinclude -c test/Packager.ut.cpp -o build/test/Packager.ut.o -g3
//now the src obj
g++ -std=c++14 -Wall -pthread -Iinclude -c src/ClientAddress.cpp -o build/debug/ClientAddress.o -g3
g++ -std=c++14 -Wall -pthread -Iinclude -c src/Packager.cpp -o build/debug/Packager.o -g3
g++ -std=c++14 -Wall -pthread -Iinclude -c src/Package.cpp -o build/debug/Package.o -g3
Built debug object files.
//now the first test itself
g++ -std=c++14 -Wall -pthread -Iinclude -lsfml-network build/test/Packager.ut.o build/debug/ClientAddress.o build/debug/Packager.o build/debug/Package.o -g3 -o bin/test/Packager.ut
build/test/Packager.ut.o: In function `main':
/home/XXX/Documents/dev/cpp/proj/beep/test/Packager.ut.cpp:69: undefined reference to `sf::IpAddress::IpAddress(char const*)'
build/debug/ClientAddress.o: In function `nw::udp::ClientAddress::ClientAddress()':
/home/XXX/Documents/dev/cpp/proj/beep/src/ClientAddress.cpp:21: undefined reference to `sf::IpAddress::IpAddress(char const*)'
build/debug/ClientAddress.o: In function `nw::udp::operator==(nw::udp::ClientAddress const&, nw::udp::ClientAddress const&)':
/home/XXX/Documents/dev/cpp/proj/beep/src/ClientAddress.cpp:33: undefined reference to `sf::operator==(sf::IpAddress const&, sf::IpAddress const&)'
...
and so on ... every mentionings of sf:: inside the files are quoted
I get the same error pattern if I try to compile the other tests (for ClientAddress for example)
Of course i now want to know what i linked wrong how there. As you can see the lib is linked with -lsfml-network. I also checked the SMFL installation, so it is at least less likely a lib file gone missing from standard directory.
I guess there is an error in my usage of g++ , compiling and linking orders or smth.
My project tree:
>bin
----mainexec
--->test
----.ut
>build
--->debug
----.o
--->release
----.o
--->test
----.ut.o
>src
---- .cpp
>include
---- .h
>test
---- .ut.cpp
As a second part of the question, I'd like to ask if there is a better way to build tests, because I simply link every src-obj with my test-obj, even if there are way more src-obj linked than it actually needs. It should work and I do not have to maintain my test dependencies all the time, which later would be very cumbersome. What is common ?
sfml-network has a dependency on sfml-system. Try add -lsfml-system before -lsfml-network in your linker command in your makefile

Undefined reference to 'dlsym' and 'dlopen'

I am compiling using arm-linux-gnueabi-g++ version 4.7.3.
I have the arm-linux-gnueabi libraries installed at location:
/usr/arm-linux-gnueabi/lib, it contains libdl.a, libdl.so, libdl.so.2,
and libdl-2.19.so.
libdl.so links to libdl.so.2 which links to libdl-2.19.so.
I am trying to link against the dl library (see command string below), but I always get the undefined reference errors.
arm-linux-gnueabi-g++ -I. -I../ -I../Comms/Linux -Wall -DLINUX -fpic -o ../../work/MyProgram main.o
-L../../work -L/usr/arm-linux-gnueabi/lib -lComms -lConsole -lUtilities -ldl
../../work/libUtilities.so: undefined reference to `dlsym'
../../work/libUtilities.so: undefined reference to `dlopen'
collect2: error: ld returned 1 exit status
If I compile using g++ 4.8.2 using the following commend then my program compiles, links, and executes fine.
g++ -I. -I../ -I../Comms/Linux -Wall -DLINUX -fpic -o ../../work/MyProgram main.o
-L../../work -lComms -lConsole -lUtilities -ldl
Obviously it can't find the libdl.so library; I thought that by adding the path to the location of the appropriate library by using the -L flag would fix the problem, but it didn't.
What am I missing with the ARM compiler command?
Well, I found the answer, I needed -Wl,--no-as-needed flag before the -ldl. I had run across this flag before I asked the question, but apparently mistyped it because it hadn't worked for me.
I don't understand why the flag is needed, but the code does finish linking now.
A SO user here says that it has to do with recent (2013 as of the user's post) versions of gcc linking to --as-needed.

Undefined reference errors when including Rcpp.h

I am using 64bit Ubuntu, and I am trying to write C++.
I discovered if I use #include <Rcpp.h>, I don't even need to call any functions in the R namespace, and I would already receive undefired reference errors:
obj/x.o: In function `Rcpp::Rstreambuf<true>::xsputn(char const*, long)':
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h:61: undefined reference to `Rprintf'
obj/x.o: In function `Rcpp::Rstreambuf<false>::xsputn(char const*, long)':
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h:65: undefined reference to `REprintf'
obj/x.o: In function `Rcpp::Rstreambuf<true>::overflow(int)':
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h:70: undefined reference to `Rprintf'
obj/x.o: In function `Rcpp::Rstreambuf<false>::overflow(int)':
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h:74: undefined reference to `REprintf'
obj/x.o: In function `Rcpp::Rstreambuf<true>::sync()':
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h:79: undefined reference to `R_FlushConsole'
obj/x.o: In function `Rcpp::Rstreambuf<false>::sync()':
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h:83: undefined reference to `R_FlushConsole'
I have installed r-base and r-base-dev. I installed Rcpp by running R as root and did a install.package("Rcpp")
I compile the C++ program using g++ with -I/usr/local/lib/R/site-library/Rcpp/include
What am I missing here? Thanks for any replies.
Just pulling Rcpp headers is not enough. You also need R headers and linking against R's library. You could use e.g. R CMD SHLIB to do this for you.
However, I'd suggest you either:
create a package that has LinkingTo: Rcpp etc ... (see Rcpp's documentation).
use sourceCpp on your .cpp file. See ?sourceCpp
As Romain Francois points out, you can't just have the headers (declarations), you also need the implementation.
What I suggest is to make a Makefile that produces a .so object. The R CMD SHLIB command is a good starting place to figure out which flags are needed, but it can't handle functions designed to be executed outside of Matlab.
Then, you need to find Rcpp.so and libR.so and link against both of them in your g++ call.
So, there is hope for "using R outside of R" -- in my case, I was able to compile some stuff from the VineCopula package into a .so file that Matlab was able to read.
See below for my Makefile (just as an example):
CFLAGS=-I/usr/share/R/include/ -I/usr/local/lib/R/site-library/Rcpp/include/ -I/usr/local/lib/R/site-library/VineCopula/include -dynamiclib -Wl,-headerpad_max_install_names -shared -L/usr/lib/R/lib -lR
CFLAGS2=-I/usr/share/R/include/ -I/usr/local/lib/R/site-library/Rcpp/include/ -I/usr/local/lib/R/site-library/VineCopula/include
LDFLAGS=-DNDEBUG -fpic -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g
all: RVinePDF.so
RVinePDF.so: RVinePDF.o Rcpp.so libR.so
g++ $(CFLAGS) -o RVinePDF.so RVinePDF.o Rcpp.so libR.so $(LDFLAGS)
\rm *.o
RVinePDF.o: RVinePDF.cpp
g++ $(CFLAGS2) -o RVinePDF.o -c RVinePDF.cpp $(LDFLAGS)
[other .o files defined similarly]

Error while executing make command to create moses\scripts\training\memscore

I have to implement the machine translation system hence I am planning use moses but I facing following error while executing make command on Cygwin:
Administrator#diebold-69b7050 /cygdrive/c/JT/NewSetup/Moses/moses-2010-08-13/moses/scripts/training/memscore$ make
make all-am
make[1]: Entering directory `/cygdrive/c/JT/NewSetup/Moses/moses-2010-08-13/moses
scripts/training/memscore'
g++ -I/usr/include -Wall -ffast-math -ftrapping-math -fomit-frame-pointer -g -O2 -o memscore.exe phrasetable.o memscore.o scorer.o lexdecom.o -lz -lm
phrasetable.o: In function `_ZlsRSoRK15PhraseAlignment':
/cygdrive/c/JT/NewSetup/Moses/moses-2010-08-13/moses/scripts/training/memscore/phrasetable.cpp:111: undefined reference to `boost::system::system_category()'
phrasetable.o: In function `__tcf_0':
/cygdrive/c/JT/NewSetup/Moses/moses-2010-08-13/moses/scripts/training/memscore/datastorage.h:31: undefined reference to `boost::system::system_category()'
phrasetable.o: In function `_ZN14PhrasePairInfo12realloc_dataEj':
/usr/include/boost/pool/simple_segregated_storage.hpp:97: undefined reference to `boost::system::system_category()'
phrasetable.o: In function `_ZNK14PhrasePairInfo14get_alignmentsEv':
Please don't give me suggestion like linker error because I am completely fad up of trying linker option.
I think, I have some Cygwin->Boot library problem. Can you suggest me where I am wrong?
You are mssing -lboost_system on your compilation commands.
Some of the boost libraries are header only. Others need to be compiled. And the libraries sometimes depend on each other. In this case you are using some boost library which needs -lboost_system. Add it so that it gets linked with your project.
and it should be on this line. Where linking is done
g++ -I/usr/include -Wall -ffast-math -ftrapping-math -fomit-frame-pointer -g -O2 -o memscore.exe phrasetable.o memscore.o scorer.o lexdecom.o **-lboost_system** -lz -lm

makefile not working with c++ zkcm-library - can't find mpfr-functions

I have a makefile for use with some programs using the zkcm-library, including the following object line:
zkcm_c.o: zkcm_c.cpp zkcm_c.h config.h
g++ -c zkcm_c.cpp -lgmp -lgmpxx -lmpfr
The compiler complains that there is an "undefined reference to '_imp_mpfr__inits2' " and the same for every use of mpfr-functions. The strange thing is, there are other objects in the makefile that uses mpfr-functions with no problem, e.g. the main program (first line):
SpMC: SpMC.cpp MCutility.h HPutility.h wfs.h MC_funcs.h $(ZKCMOBJECTS) $(ZKCMHFILES)
g++ SpMC.cpp -lgmp -lmpfr -lmpc -o SpMC $(ZKCMOBJECTS)
In both cases I have #include<mpfr.h> in the source and as you see link to it using -lmpfr. Any ideas why this works for SpMC.cpp but not zkcm_c.cpp?