Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recompile with -fPIC option, but the option is already in the makefile

Tags:

I get this error when I do the make:

relocation R_X86_64_32 against `vtable for Torch::MemoryDataSet' can not be used  when making a shared object; recompile with -fPIC 

It says that I should recompile with the -fPIC option. I did that, adding the -fPIC option to CFLAGS and CXXFLAGS, but I still get the same error. Is there any way to solve this? I have seen that this problem is related with the use of a 64-bit machine, and it is true that I am using one.

like image 777
Eduardo Avatar asked Dec 02 '08 01:12

Eduardo


People also ask

How do I recompile with makefile?

Use the command `make' to recompile the source files that really need recompilation. Make the changes in the header files. Use the command `make -t' to mark all the object files as up to date. The next time you run make, the changes in the header files do not cause any recompilation.

What does $< mean in makefile?

The $@ and $< are called automatic variables. The variable $@ represents the name of the target and $< represents the first prerequisite required to create the output file.

How do I clean my makefile?

The Cleanup Rule clean: rm *.o prog3 This is an optional rule. It allows you to type 'make clean' at the command line to get rid of your object and executable files. Sometimes the compiler will link or compile files incorrectly and the only way to get a fresh start is to remove all the object and executable files.

Why does makefile keep relinking?

In makefiles for substantial projects, the rules and lists of files are often more complicated, using multiple symbols to convey commands, options, and lists of files. In this context, “relink” merely means that make will execute the command to link objects into an executable again.


2 Answers

I had this problem quite a while back and if I remember correctly, the fix was moving the placement of -fPIC just after gcc in the command line. Made absolutely no sense, and less so now, but as I remember, that fixed it.

like image 189
clintm Avatar answered Sep 25 '22 00:09

clintm


I encountered the same problem, but it had an extra twist. The answer by @clintm solved it, but I thought I would describe my variation of the problem here for future reference...

Makefile on 32-bit machine:

CXX=g++ CXXFLAGS= -O3 -Wall ... ...    %.o:  %.c     $(CXX)  $(CXXFLAGS)  -fpic  -c  $<        libmylibrary.so: $(OBJECTS)     $(CXX) -shared -Wl,-soname,$@ -o $@   $(OBJECTS) 

This compiled correctly. But the same Makefile failed when I tried it on a 64-bit machine. I changed "-fpic" to "-fPIC" and it still failed. I changed the object rule to:

%.o:  %.c     $(CXX)  -fPIC  $(CXXFLAGS)  -c  $<  

and it still failed.

Finally, I placed "-fPIC" in the actual compiler variable (so that now "-fPIC" appears in the rule for each object and the rule for the shared library):

CXX=g++  -fPIC CXXFLAGS= -g -O3 -Wall ... %.o:  %.c         $(CXX)    $(CXXFLAGS)   -c      -o $@    $<     libalglib.so: $(OBJECTS)         $(CXX) -shared -Wl,-soname,$@  -o $@      $(OBJECTS) 

And it worked!

like image 32
cmo Avatar answered Sep 24 '22 00:09

cmo