Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross compiling C++ project, Relocations in generic ELF (EM: 3)

I've been working on a c++ project for a while now, but would like to port it over to my arm processor. I already have all of my cross-compile tools (I'm using CodeSourcery) and thought I could just change my makefile to point to that compiler. It compiles fine using the default g++, but When try a make pointing to the cross-compiler I get relocation errors:

/home/oryan/CodeSourcery/bin/../lib/gcc/arm-none-linux-gnueabi/4.5.2/../../../../arm-none-linux-gnueabi/bin/ld: ServerSocket.o: Relocations in generic ELF (EM: 3)
ServerSocket.o: could not read symbols: File in wrong format
collect2: ld returned 1 exit status
make: *** [simple_server] Error 1

It seems like I don't have a proper link set up or it's pointing to a wrong location. I'm not that familiar with makefiles and am probably missing something obvious. The makefile I've been using is from http://tldp.org/LDP/LG/issue74/tougher.html with the client side removed:

# Makefile for the socket programming example
#

simple_server_objects = ServerSocket.o Socket.o simple_server_main.o

all : simple_server

simple_server: $(simple_server_objects)
         /home/matt/CodeSourcery/bin/arm-none-linux-gnueabi-g++ -o simple_server $(simple_server_objects)


Socket: Socket.cpp
ServerSocket: ServerSocket.cpp
simple_server_main: simple_server_main.cpp

clean:
        rm -f *.o simple_server

Right now I am manually compiling each file and it works great, but I'd like to further my understanding here.

Thanks!

like image 949
Matt Avatar asked Nov 17 '11 14:11

Matt


People also ask

What is cross compilation in C?

Cross-compilation is the act of compiling code for one computer system (often known as the target) on a different system, called the host. It's a very useful technique, for instance when the target system is too small to host the compiler and all relevant files.

Why is cross compiling so hard?

"building a cross-compiler is significantly harder than building a compiler that targets the platform it runs on." The problem exists due to the way libraries are built and accessed. In the normal situation all the libraries are located in a specific spot, and are used by all apps on that system.

What is cross compiling GCC?

To cross-compile is to build on one platform a binary that will run on another platform.


1 Answers

The problem is you've set your makefile up to link with the new g++ but you haven't changed the compiler you're using to build the objects in the first place.

The easiest way to fix this is to set environment CXX to the next compiler, i.e.

export CXX=/home/matt/CodeSourcery/bin/arm-none-linux-gnueabi-g++

or just set it for a given make by adding CXX=... to the command line.

You'll need to make clean first but you'll then use the correct compiler for both the compile and link.

You could also specify a new how-to-compile-C++ files rule in your makefile to specify the new compiler but the environment variable is easier:

.cc.o:
        /home/.../g++ $(CPPFLAGS) $(CXXFLAGS) -c
like image 185
Rup Avatar answered Oct 27 '22 00:10

Rup