4
votes

I am a bit of a newbie as far as dealing with compilation/linking errors.

I'm working with a large C++ code (there are a few C files as well). I have successfully been running it on a Mac, compiled with g++. Now I need to run it on a Linux-based cluster because it is too slow on my Mac. The code consists of several libraries that I have to compile, plus my own code that uses the libraries.

I can compile all of the code on the cluster using the default g++ compiler. However, unfortunately, I have discovered that I need to compile with gcc/4.7.2 so that the code will work with other software on the cluster. I have been working my way through compilation and linking errors. So far, they have all been related to issues with C++ versus C. For example, I have had to add "extern" in some of the C files. I have had to change includes from C++ to C headers.

My problem seems to be with one particular library (I have successfully dealt with all the others). It compiles in its directory. But when I go to my run directory, I get all kinds of errors that seem to be related to the code that makes up this problem library. My guess is that they are mostly related to not finding the standard libraries. I just don't understand how there is a problem with my includes and would really appreciate it if someone could take a look at what I have in my makefile for this library. This library is officially based on "C++," but there does seem to be a lot of C-style code in its files.

There are far too many errors (pages) and far too much code to post everything. I hope that what I have posted is sufficient and can add to it if that would help. I can't locate exactly where they are coming from in my code because they are very cryptic; for example:

Code.cpp:(.text+0x35): undefined reference to `std::cout'

My makefile include and compiler options are below. I originally created this on my Mac using QT. I modified it to work with the Linux cluster on gcc/4.7.2. So it is entirely possible that it is a bit of a mess.

First I do (command line in terminal):

module load gcc/4.7.2

Then the makefile is:

CC = gcc

DEFINES = -DIPMGEMPLUGIN -DNOPARTICLEARRAY -D__unix

CFLAGS = -c -g -O2 -pedantic -fno-nonansi-builtins -D__unix -m64

CXXFLAGS = -pipe -O2 -Wall -W -fPIC $(DEFINES) -lstdc++ -m64

INCPATH = -I. \

             -I/mounts/apps/gcc/4.7.2/ \

             -I/mounts/apps/gcc/4.7.2/bin/ \

             -I/mounts/apps/gcc/4.7.2/bin/x86_64-unknown-linux-gnu/4.7.2 \

             -I/mounts/apps/gcc/4.7.2/lib64 \

             -I/mounts/apps/gcc/4.7.2/bin/include \

             -I/mounts/apps/gcc/4.7.2/bin/include/c++ \

             -I/mounts/apps/gcc/4.7.2/4.7.2/bin/include/c++/4.7.2 \

AR = ar cq RANLIB = ranlib -s TARGET = mylib.a

.SUFFIXES: .o .c .cpp .cc .cxx .C

.cpp.o: $(CC) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.cc.o: $(CC) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.cxx.o: $(CC) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.C.o: $(CC) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.c.o: $(CC) -c $(CFLAGS) $(INCPATH) -o "$@" "$<"

Now the kinds of errors I'm getting:

Code.o: In function _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.part.8': Code.cpp:(.text+0x12): undefined reference tostd::basic_ios >::clear(std::_Ios_Iostate)' Code.o: In function _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.constprop.101': Code.cpp:(.text+0x35): undefined reference tostd::cout' Code.cpp:(.text+0x3a): undefined reference to `std::basic_ostream >& std::__ostream_insert

(std::basic_ostream >&, char const*, long)' Code.cpp:(.text+0x3f): undefined reference to std::cout' Code.cpp:(.text+0x49): undefined reference tostd::cout' Code.cpp:(.text+0x53): undefined reference to std::cout' Code.o: In function _ZNSt14basic_ofstreamIcSt11char_traitsIcEE4openEPKcSt13_Ios_Openmode.constprop.99': Code.cpp:(.text+0x93): undefined reference to std::basic_filebuf<char, std::char_traits<char> >::open(char const*, std::_Ios_Openmode)' Code.o: In function _ZNSt18basic_stringstreamIcSt11char_traitsIcESaIcEEC1ESt13_Ios_Openmode.constprop.96': Code.cpp:(.text+0x103): undefined reference to std::ios_base::ios_base()' Code.cpp:(.text+0x10b): undefined reference tovtable for std::basic_ios ' Code.cpp:(.text+0x11b): undefined reference to VTT for std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >' Code.cpp:(.text+0x15d): undefined reference to std::basic_iostream >::basic_iostream()' Code.cpp:(.text+0x16c): undefined reference to vtable for std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >' Code.cpp:(.text+0x174): undefined reference to vtable for std::basic_stringstream, std::allocator >' Code.cpp:(.text+0x17c): undefined reference to vtable for std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >' Code.cpp:(.text+0x184): undefined reference to vtable for std::basic_streambuf >'

And further on:

gl3.cpp:(.text+0x18d): undefined reference to `operator new[](unsigned long)'

gl3.cpp:(.text+0x1a1): undefined reference to `operator new[](unsigned long)'

gl3.cpp:(.text+0x1b5): undefined reference to `operator new[](unsigned long)'

gl3.cpp:(.text+0x1c9): undefined reference to `operator new[](unsigned long)'

gl3.cpp:(.text+0x1dd): undefined reference to `operator new[](unsigned long)'

/data/place/number/account/CodeDirectory/../ProblemLibraryDirectory/libProblem.a(gl3.o): In function Other::free_internal()': gl3.cpp:(.text+0x251): undefined reference tooperator delete' gl3.cpp:(.text+0x262): ...

Does any of this mean anything to anyone?

1
You say " It compiles. But when I go to my run directory, I get all kinds of errors. " But then you show a compile error, Code.cpp:(.text+0x35): undefined reference to `std::cout' Are you sure you #included <iostream> in Code.cpp?doctorlove
Thanks for your reply, doctorlove. What I mean is, the libraries all compile in their directories. When I go to my run directory (that uses the libraries), compilation fails. I have checked code.cpp and there is "#include <iostream>." The errors all seem to go back to the problem library. Above (the last text in a grey background), "gl3.cpp" (and the other .cpp files in other error output I haven't shown because it's too long) are in the problem library.Ant
If you use g++ instead of gcc this probably won't happen. gcc compiler needs to be linked with standard c++ libraries but by using g++ they are linked automatically. Also I would suggest using CMake instead of make.Kourosh
Thanks Kourosh. Yes, I have no problem when I compile using g++, but unfortunately, I must use gcc. To use CMake, would I simple type "CMake" instead of "make"? I have to leave my computer for a little while, but will be back in a few hours.Ant
@Ant: CMake uses completely different style "makefiles". For a project your size, I'm far from convinced that CMake makes more sense than Make.Mats Petersson

1 Answers

1
votes

You are using C++ functions such as new and std::* functions, therefore you must compile using a C++ compiler (unless you link the C compiled version with the C++ libraries). GCC is a C compiler and G++ is the GNU C++ compiler, so you should use G++.

I saw in one of your comments that for some reason you had to use GCC. If this is the case then you will have to remove all C++ specific code from your program (e.g. std::* and new/delete), or link with the C++ libraries. However I don't see any reason that you should have to use GCC. Please explain why you have to use GCC then we may be able to help you further.