C ++ warning: relocation against ... undefined reference to - c++

I've a problem compiling my C++ project using a makefile.
The structure of the project is like:
gui/:
Basicshape.cpp / .h
Functionable.cpp / .h
logic/:
Matrix.cpp/ .h
Cell.cpp/ .h
Item.cpp / .h
Position.cpp / .h
main.cpp
makefile
Here's the content of the makefile:
FLAGS=--std='c++20' -Wall -Wextra -Wpedantic -lfltk
all:CandyCrush
CandyCrush: main.o Matrice.o Cell.o Item.o Position.o BasicShape.o Functionable.o
g++ main.o Matrice.o Cell.o Item.o Position.o BasicShape.o Functionable.o -o CandyCrush
Cell.o: logic/Cell.h logic/Cell.cpp
g++ $(FLAGS) -c logic/Cell.cpp -o Cell.o
Item.o: logic/Item.h logic/Item.cpp
g++ $(FLAGS) -c logic/Item.cpp -o Item.o
Position.o: logic/Position.h logic/Position.cpp
g++ $(FLAGS) -c logic/Position.cpp -o Position.o
Matrice.o: logic/Matrice.h logic/Matrice.cpp
g++ $(FLAGS) -c logic/Matrice.cpp -o Matrice.o
BasicShape.o: gui/BasicShapes.h gui/BasicShapes.cpp
g++ $(FLAGS) -c gui/BasicShapes.cpp -o BasicShape.o
Functionable.o: gui/Functionable.h gui/Functionable.cpp
g++ $(FLAGS) -c gui/Functionable.cpp -o Functionable.o
main.o: main.cpp
g++ $(FLAGS) -c main.cpp -o main.o
clear:
rm *.o
I know it can be shortened but for debugging purposes and the fact than I'm still learning I prefer keeping this structure.
By typing make in my terminal, I've this error and I'm not sure why :
/usr/bin/ld: BasicShape.o: warning: relocation against `_ZTV9Rectangle' in read-only section `.text'
/usr/bin/ld: BasicShape.o: in function `Rectangle::Rectangle(Point, int, int, unsigned int, unsigned int)':
BasicShapes.cpp:(.text+0xdd): undefined reference to `vtable for Rectangle'
/usr/bin/ld: BasicShapes.cpp:(.text+0xf3): undefined reference to `vtable for Rectangle'
/usr/bin/ld: BasicShape.o: in function `Circle::Circle(Point, int, unsigned int, unsigned int)':
BasicShapes.cpp:(.text+0x225): undefined reference to `vtable for Circle'
/usr/bin/ld: BasicShapes.cpp:(.text+0x23b): undefined reference to `vtable for Circle'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
make: *** [makefile:6: CandyCrush] Error 1

Related

Why gcc cant complle this file containing virtual class ,but clion IDE can do that? [duplicate]

Shall this be the example:
#include <iostream>
using namespace std;
int main()
{
cout << "Hola, moondo.\n";
}
It throws the error:
gcc -c main.cpp gcc -o edit main.o main.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `std::cout'
main.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char,std::char_traits<char> >& std::operator<< <std::char_traits<char>>(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
main.o: In function `__static_initialization_and_destruction_0(int,int)':
main.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()' collect2: error: ld
returned 1 exit status make: *** [qs] Error 1
Also, this example:
#include <iostream>
int main()
{
std::cout << "Hola, moondo.\n";
}
throws the error:
gcc -c main.cpp gcc -o edit main.o main.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `std::cout'
main.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char,std::char_traits<char> >& std::operator<<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char> >&, char const*)'
main.o: In function `__static_initialization_and_destruction_0(int,int)': main.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()' collect2: error: ld
returned 1 exit status make: *** [qs] Error 1
Note: I am using Debian 7 (Wheezy).
Compile the program with:
g++ -Wall -Wextra -Werror -c main.cpp -o main.o
^^^^^^^^^^^^^^^^^^^^ <- For listing all warnings when your code is compiled.
as cout is present in the C++ standard library, which would need explicit linking with -lstdc++ when using gcc; g++ links the standard library by default.
With gcc, (g++ should be preferred over gcc)
gcc main.cpp -lstdc++ -o main.o
Yes, using g++ command worked for me:
g++ my_source_code.cpp
Assuming code.cpp is the source code, the following will not throw errors:
make code
./code
Here the first command compiles the code and creates an executable with the same name, and the second command runs it. There is no need to specify g++ keyword in this case.
Makefiles
If you're working with a makefile and you ended up here like me, then this is probably what you're looking or:
If you're using a makefile, then you need to change cc as shown below
my_executable : main.o
cc -o my_executable main.o
to
CC = g++
my_executable : main.o
$(CC) -o my_executable main.o
Adding the following line in your CMake makes gcc link with std and therefore recognize std::cout
target_link_libraries(your_project
PRIVATE
-lstdc++
)
FWIW, if you want a makefile, here is how you can do either answer by switching the compiler at the top.
# links stdc++ library by default
# CC := g++
# or
CC := cc
all: hello
util.o: util.cc
$(CC) -c -o util.o util.cc
main.o: main.cc
$(CC) -c -o main.o main.cc
# notice -lstd++ is after the .o files
hello: main.o util.o
$(CC) -o hello main.o util.o -lstdc++
clean:
-rm util.o main.o hello

CMake "undefined reference" [duplicate]

Shall this be the example:
#include <iostream>
using namespace std;
int main()
{
cout << "Hola, moondo.\n";
}
It throws the error:
gcc -c main.cpp gcc -o edit main.o main.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `std::cout'
main.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char,std::char_traits<char> >& std::operator<< <std::char_traits<char>>(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
main.o: In function `__static_initialization_and_destruction_0(int,int)':
main.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()' collect2: error: ld
returned 1 exit status make: *** [qs] Error 1
Also, this example:
#include <iostream>
int main()
{
std::cout << "Hola, moondo.\n";
}
throws the error:
gcc -c main.cpp gcc -o edit main.o main.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `std::cout'
main.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char,std::char_traits<char> >& std::operator<<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char> >&, char const*)'
main.o: In function `__static_initialization_and_destruction_0(int,int)': main.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()' collect2: error: ld
returned 1 exit status make: *** [qs] Error 1
Note: I am using Debian 7 (Wheezy).
Compile the program with:
g++ -Wall -Wextra -Werror -c main.cpp -o main.o
^^^^^^^^^^^^^^^^^^^^ <- For listing all warnings when your code is compiled.
as cout is present in the C++ standard library, which would need explicit linking with -lstdc++ when using gcc; g++ links the standard library by default.
With gcc, (g++ should be preferred over gcc)
gcc main.cpp -lstdc++ -o main.o
Yes, using g++ command worked for me:
g++ my_source_code.cpp
Assuming code.cpp is the source code, the following will not throw errors:
make code
./code
Here the first command compiles the code and creates an executable with the same name, and the second command runs it. There is no need to specify g++ keyword in this case.
Makefiles
If you're working with a makefile and you ended up here like me, then this is probably what you're looking or:
If you're using a makefile, then you need to change cc as shown below
my_executable : main.o
cc -o my_executable main.o
to
CC = g++
my_executable : main.o
$(CC) -o my_executable main.o
Adding the following line in your CMake makes gcc link with std and therefore recognize std::cout
target_link_libraries(your_project
PRIVATE
-lstdc++
)
FWIW, if you want a makefile, here is how you can do either answer by switching the compiler at the top.
# links stdc++ library by default
# CC := g++
# or
CC := cc
all: hello
util.o: util.cc
$(CC) -c -o util.o util.cc
main.o: main.cc
$(CC) -c -o main.o main.cc
# notice -lstd++ is after the .o files
hello: main.o util.o
$(CC) -o hello main.o util.o -lstdc++
clean:
-rm util.o main.o hello

What does a gcc error about undefined references, what does it mean? [duplicate]

Shall this be the example:
#include <iostream>
using namespace std;
int main()
{
cout << "Hola, moondo.\n";
}
It throws the error:
gcc -c main.cpp gcc -o edit main.o main.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `std::cout'
main.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char,std::char_traits<char> >& std::operator<< <std::char_traits<char>>(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
main.o: In function `__static_initialization_and_destruction_0(int,int)':
main.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()' collect2: error: ld
returned 1 exit status make: *** [qs] Error 1
Also, this example:
#include <iostream>
int main()
{
std::cout << "Hola, moondo.\n";
}
throws the error:
gcc -c main.cpp gcc -o edit main.o main.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `std::cout'
main.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char,std::char_traits<char> >& std::operator<<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char> >&, char const*)'
main.o: In function `__static_initialization_and_destruction_0(int,int)': main.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()' collect2: error: ld
returned 1 exit status make: *** [qs] Error 1
Note: I am using Debian 7 (Wheezy).
Compile the program with:
g++ -Wall -Wextra -Werror -c main.cpp -o main.o
^^^^^^^^^^^^^^^^^^^^ <- For listing all warnings when your code is compiled.
as cout is present in the C++ standard library, which would need explicit linking with -lstdc++ when using gcc; g++ links the standard library by default.
With gcc, (g++ should be preferred over gcc)
gcc main.cpp -lstdc++ -o main.o
Yes, using g++ command worked for me:
g++ my_source_code.cpp
Assuming code.cpp is the source code, the following will not throw errors:
make code
./code
Here the first command compiles the code and creates an executable with the same name, and the second command runs it. There is no need to specify g++ keyword in this case.
Makefiles
If you're working with a makefile and you ended up here like me, then this is probably what you're looking or:
If you're using a makefile, then you need to change cc as shown below
my_executable : main.o
cc -o my_executable main.o
to
CC = g++
my_executable : main.o
$(CC) -o my_executable main.o
Adding the following line in your CMake makes gcc link with std and therefore recognize std::cout
target_link_libraries(your_project
PRIVATE
-lstdc++
)
FWIW, if you want a makefile, here is how you can do either answer by switching the compiler at the top.
# links stdc++ library by default
# CC := g++
# or
CC := cc
all: hello
util.o: util.cc
$(CC) -c -o util.o util.cc
main.o: main.cc
$(CC) -c -o main.o main.cc
# notice -lstd++ is after the .o files
hello: main.o util.o
$(CC) -o hello main.o util.o -lstdc++
clean:
-rm util.o main.o hello

Irregular Makefile Behaviour?

I'm compiling a program with 3 source files: main.cpp, DataHandler.cpp and DataHandler.h.
I use a makefile that contains the following code to build:
Makefile
OBJECTS = main.o DataHandler.o
CC = g++
DEBUG = -g
CFLAGS = -c -std=c++11
LFLAGS = -lopendnp3 -lopenpal -lasiodnp3 -lasiopal -lpthread -std=c++11
lvoutstation : $(OBJECTS)
$(CC) $(LFLAGS) $(OBJECTS) -o lvoutstation
main.o : main.cpp DataHandler.h
$(CC) $(CFLAGS) main.cpp
DataHandler.o : DataHandler.cpp DataHandler.h
$(CC) $(CFLAGS) DataHandler.cpp
.PHONY : clean
clean :
rm lvoutstation $(OBJECTS)
It build main.o and DataHandler.o fine, but when it gets to compiling the action executable, it gives linking erros to all my asiodnp3 and opendnp3 namespace function / class calls.
When I run the following command:
g++ -o lvoutstation main.o DataHandler.o -lopendnp3 -lopenpal -lasiodnp3
-lasiopal -lpthread -std=c++11
It works fine..
I don't understand where the linking error comes in?
EDIT
Upon request for the error message:
g++ -lopendnp3 -lopenpal -lasiodnp3 -lasiopal -std=c++11 main.o DataHandler.o -o lvoutstation
main.o: In function `main':
main.cpp:(.text+0x85): undefined reference to `asiodnp3::DNP3Manager::DNP3Manager(unsigned int, openpal::ICryptoProvider*, std::function<void ()>, std::function<void ()>)'
main.cpp:(.text+0xb7): undefined reference to `asiodnp3::DNP3Manager::AddLogSubscriber(openpal::ILogHandler*)'
main.cpp:(.text+0xf0): undefined reference to `opendnp3::ChannelRetry::Default()'
main.cpp:(.text+0x123): undefined reference to `asiodnp3::DNP3Manager::AddTCPServer(char const*, unsigned int, opendnp3::ChannelRetry const&, std::string const&, unsigned short)'
main.cpp:(.text+0x19f): undefined reference to `opendnp3::EventBufferConfig::AllTypes(unsigned short)'
main.cpp:(.text+0x1d6): undefined reference to `opendnp3::DefaultOutstationApplication::Instance()'
main.cpp:(.text+0x2c8): undefined reference to `asiodnp3::DNP3Manager::~DNP3Manager()'
main.o: In function `opendnp3::LinkConfig::LinkConfig(bool, bool)':
main.cpp:(.text._ZN8opendnp310LinkConfigC2Ebb[_ZN8opendnp310LinkConfigC5Ebb]+0x75): undefined reference to `openpal::TimeDuration::Seconds(long)'
main.cpp:(.text._ZN8opendnp310LinkConfigC2Ebb[_ZN8opendnp310LinkConfigC5Ebb]+0x87): undefined reference to `openpal::TimeDuration::Minutes(long)'
main.o: In function `opendnp3::OutstationConfig::OutstationConfig()':
main.cpp:(.text._ZN8opendnp316OutstationConfigC2Ev[_ZN8opendnp316OutstationConfigC5Ev]+0x14): undefined reference to `opendnp3::OutstationParams::OutstationParams()'
main.cpp:(.text._ZN8opendnp316OutstationConfigC2Ev[_ZN8opendnp316OutstationConfigC5Ev]+0x56): undefined reference to `opendnp3::EventBufferConfig::EventBufferConfig(unsigned short, unsigned short, unsigned short, unsigned short, unsigned short, unsigned short, unsigned short, unsigned short)'
main.o: In function `asiodnp3::ConsoleLogger::Instance()':
main.cpp:(.text._ZN8asiodnp313ConsoleLogger8InstanceEv[_ZN8asiodnp313ConsoleLogger8InstanceEv]+0x5): undefined reference to `asiodnp3::ConsoleLogger::instance'
DataHandler.o: In function `DataHandler::ReadMeasurements(asiodnp3::IOutstation*)':
DataHandler.cpp:(.text+0xfe): undefined reference to `asiodnp3::MeasUpdate::MeasUpdate(asiodnp3::IOutstation*)'
DataHandler.cpp:(.text+0x11d): undefined reference to `opendnp3::Analog::Analog(double)'
DataHandler.cpp:(.text+0x137): undefined reference to `asiodnp3::MeasUpdate::Update(opendnp3::Analog const&, unsigned short, opendnp3::EventMode)'
DataHandler.cpp:(.text+0x159): undefined reference to `asiodnp3::MeasUpdate::~MeasUpdate()'
DataHandler.cpp:(.text+0x17b): undefined reference to `asiodnp3::MeasUpdate::~MeasUpdate()'
collect2: error: ld returned 1 exit status
make: *** [lvoutstation] Error 1
The makefile just works as expected. Your action becomes actually different from your command line sample
$(CC) $(LFLAGS) $(OBJECTS) -o lvoutstation
will expand to
g++ -lopendnp3 -lopenpal -lasiodnp3 -lasiopal -lpthread -std=c++11 \
main.o DataHandler.o -o lvoutstation
The order of object files and libraries matters for the linking process.
You can fix the linker errors providing the libraries after the object files:
$(CC) $(OBJECTS) $(LFLAGS) -o lvoutstation

Program won't compile anymore

I'm current working on a C++ that I edit locally on my Mac but run on an Ubuntu server. I always make sure that the code compiles on my mac before uploading it to the server to compile it there, where I have to use a makefile to link with libraries that are installed in my local directory. Basically, I had edited a significant portion of my code, found that it compiled on my mac, and uploaded it to the server to compile, but it doesn't compile! Luckily, I had a backup version of the code, so I tried that on the server, and that won't compile anymore either! In between the last time I knew that my code compiled on the server and now, I know that they ran some updates, but that's all I can think of that's different. For reference, here's my make file:
LOCAL_INCLUDE = /home/schraiber/.local/include
LOCAL_LIB = /home/schraiber/.local/lib
CXXFLAGS = -I$(LOCAL_INCLUDE)
CXX_LDFLAGS = -L$(LOCAL_LIB) -lgsl -lm -lgslcblas -lpthread
CoalHMMgf: main.o IOUtilities.o parameters.o Algorithms.o Optimization.o probabilities.o RNGUtilities.o Data.o ThreadData.h
g++ $(CXXFLAGS) $(CXX_LDFLAGS) main.o IOUtilities.o parameters.o Algorithms.o Optimization.o probabilities.o RNGUtilities.o Data.o -o CoalHMMgf
main.o: main.cpp IOUtilities.h parameters.h Algorithms.h Optimization.h probabilities.h ThreadData.h Data.h
g++ $(CXXFLAGS) $(CXX_LDFLAGS) -c main.cpp
IOutilities.o: IOUtilities.h IOUtilities.cpp parameters.h data.h
g++ $(CXXFLAGS) $(CXX_LDFLAGS) -c IOUtilities.cpp
parameters.o: parameters.h parameters.cpp
g++ $(CXXFLAGS) $(CXX_LDFLAGS) -c parameters.cpp
Algorithms.o: Algorithms.h Algorithms.cpp
g++ $(CXXFLAGS) $(CXX_LDFLAGS) -c Algorithms.cpp
Optimization.o: Optimization.h Optimization.cpp Algorithms.h parameters.h probabilities.h RNGUtilities.h ThreadData.h IOUtilities.h
g++ $(CXXFLAGS) $(CXX_LDFLAGS) -c Optimization.cpp
probabilities.o: probabilities.h probabilities.cpp parameters.h
g++ $(CXXFLAGS) $(CXX_LDFLAGS) -c probabilities.cpp
RNGUtilities.o: RNGUtilities.h RNGUtilities.cpp
g++ $(CXXFLAGS) $(CXX_LDFLAGS) -c RNGUtilities.cpp
Data.o: Data.h Data.cpp
g++ $(CXXFLAGS) $(CXX_LDFLAGS) -c Data.cpp
and the error:
g++ -I/home/schraiber/.local/include -L/home/schraiber/.local/lib -lgsl -lm -lgslcblas -lpthread main.o IOUtilities.o parameters.o Algorithms.o Optimization.o probabilities.o RNGUtilities.o Data.o -o CoalHMMgf
main.o: In function `main':
main.cpp:(.text+0xbf1): undefined reference to `pthread_create'
main.cpp:(.text+0xd0e): undefined reference to `pthread_join'
Optimization.o: In function `my_df(gsl_vector const*, void*, gsl_vector*)':
Optimization.cpp:(.text+0x3149): undefined reference to `gsl_vector_alloc'
Optimization.cpp:(.text+0x316b): undefined reference to `gsl_vector_get'
Optimization.cpp:(.text+0x3180): undefined reference to `gsl_vector_set'
Optimization.cpp:(.text+0x31bc): undefined reference to `gsl_vector_get'
Optimization.cpp:(.text+0x31ec): undefined reference to `gsl_vector_set'
Optimization.cpp:(.text+0x323b): undefined reference to `gsl_vector_set'
Optimization.cpp:(.text+0x327e): undefined reference to `gsl_vector_set'
Optimization.cpp:(.text+0x32c9): undefined reference to `gsl_vector_set'
Optimization.o: In function `MHthreaded(void*)':
Optimization.cpp:(.text+0x3489): undefined reference to `gsl_rng_env_setup'
Optimization.cpp:(.text+0x3490): undefined reference to `gsl_rng_default'
Optimization.cpp:(.text+0x360b): undefined reference to `gsl_rng_uniform'
Optimization.cpp:(.text+0x3653): undefined reference to `gsl_ran_gaussian'
Optimization.cpp:(.text+0x3686): undefined reference to `gsl_ran_gaussian'
Optimization.cpp:(.text+0x377f): undefined reference to `gsl_rng_uniform'
RNGUtilities.o: In function `AllocRNG(gsl_rng*&, gsl_rng_type const*, int)':
RNGUtilities.cpp:(.text+0x9a): undefined reference to `gsl_rng_alloc'
RNGUtilities.cpp:(.text+0xba): undefined reference to `gsl_rng_set'
RNGUtilities.o: In function `FreeRNG(gsl_rng*&)':
RNGUtilities.cpp:(.text+0xe3): undefined reference to `gsl_rng_free'
collect2: ld returned 1 exit status
make: *** [CoalHMMgf] Error 1
and just to verify that gsl is installed in the local directory:
schraiber#trump:~/test_rsync$ ls ~/.local/lib
libbpp-core.a libbpp-core.so.2.0.0 libbpp-seq.so.9 libgslcblas.a libgslcblas.so.0 libgsl.so pkgconfig
libbpp-core.so libbpp-seq.a libbpp-seq.so.9.1.0 libgslcblas.la libgslcblas.so.0.0.0 libgsl.so.0 python2.7
libbpp-core.so.2 libbpp-seq.so libgsl.a libgslcblas.so libgsl.la libgsl.so.0.16.0
schraiber#trump:~/test_rsync$ ls ~/.local/include/
Bpp gsl
An interesting thing to note is that my program is also supposed to link with Bpp, and it does that just fine as far as I can tell.
Your "-lfoo" parameters should come last on your "g++" command line. So, for example, make this change in your makefile:
CoalHMMgf: main.o IOUtilities.o parameters.o Algorithms.o Optimization.o probabilities.o RNGUtilities.o Data.o ThreadData.h
g++ $(CXXFLAGS) main.o IOUtilities.o parameters.o Algorithms.o Optimization.o probabilities.o RNGUtilities.o Data.o -o CoalHMMgf $(CXX_LDFLAGS)
Libraries have to be defined on the link line such that the library containing no other references is the very last one. Libraries that depend on code from a second library need to be listed first. They must be listed after the .o files, or else the .o files are assumed to be self-contained and don't need the libraries.