recompiling with -fPIE flag could not resolve error - c++

I am trying to compile this app using its makefile on WSL. But I get the following error:
g++ -g -Wall -ansi pendulumSystem.o TimeStepper.o particleSystem.o ClothSystem.o simpleSystem.o camera.o main.o vecmath/src/Vector3f.o vecmath/src/Vector2f.o vecmath/src/Matrix3f.o vecmath/src/Matrix4f.o vecmath/src/Vector4f.o vecmath/src/Quat4f.o vecmath/src/Matrix2f.o -o a3 -L. -lRK4 -lglut -lGL -lGLU
/usr/bin/ld: ./libRK4.a(RK4.o): relocation R_X86_64_32 against symbol `__gxx_personality_v0##CXXABI_1.3' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: ./libRK4.a(RK4.o): relocation R_X86_64_PC32 against symbol `_Znwm##GLIBCXX_3.4' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
make: *** [Makefile:15: a3] Error 1
I tried editing my makefile to include the flag like below, but it still gives me the same error message.
INCFLAGS = -I vecmath/include
INCFLAGS += -I /usr/include/GL
LINKFLAGS = -L. -lRK4 -lglut -lGL -lGLU
CFLAGS = -g -Wall -ansi -fPIE
CC = g++
SRCS = $(wildcard *.cpp)
SRCS += $(wildcard vecmath/src/*.cpp)
OBJS = $(SRCS:.cpp=.o)
PROG = a3
all: $(SRCS) $(PROG)
$(PROG): $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $# $(LINKFLAGS)
.cpp.o:
$(CC) $(CFLAGS) $< -c -o $# $(INCFLAGS)
depend:
makedepend $(INCFLAGS) -Y $(SRCS)
clean:
rm $(OBJS) $(PROG)
Could it be that the provided libRK4.a file was not compiled with the -fPIE flag?

Related

Linking dynamic library to my project

After reading lots of post I am really confused.
I want to link a dynamic library to my cpp code.
The library is in /usr/local/include/sbml
and the libsbml.so file can be found in /usr/local/lib
I have a makefile that looks like this
SHELL = /bin/sh
VERSION = 5.04.02
CC = /usr/bin/g++
CFLAGS = -Wall -g -D_REENTRANT -DVERSION=\"$(VERSION)\"
LDFLAGS = -lsbml
OBJ = main.o SBML.o
prog: $(OBJ)
$(CC) $(CFLAGS) -o prog $(OBJ) $(LDFLAGS)
%.o: %.c
$(CC) $(CFLAGS) -c $<
.PHONY : clean
clean :
-rm edit $(OBJ)
If I run the makefile I get the following error: (undefined reference to SBMLReader::readSBMLFromFile())
g++ -c -o SBML.o SBML.cpp
/usr/bin/g++ -Wall -g -D_REENTRANT -DVERSION=\"5.04.02\" -o prog main.o SBML.o -lsbml
sbml.o: In Funktion `SBML::readSBML()':
sbml.cpp:(.text+0x129): Nicht definierter Verweis auf `SBMLReader::readSBMLFromFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: error: ld returned 1 exit status
The library was not linked because you need to specify the folder containing the so-file as well:
LDFLAGS = -L/usr/local/lib -lsbml
Now it should link and your unresolved symbol should be gone as well.

What difference between my compilation line and my makefile could cause the error?

I have previously write a compilation line which is working.
Howewer, my Makefile, which generates more or less the same thing, isn't successfully compiling.
Command line (working) :
sh3eb-elf-gcc -m3 -mb -ffreestanding -nostdlib -T addin.ld src/crt0.s src/BTKOM.cpp src/bluetooth.cpp src/syscall.s -o addin.elf -Iinclude -L libs/ -lgcc -lmonochrome -lfx -O2 -fno-exceptions
Makefile :
CC = sh3eb-elf-gcc
SRCDIR = src
INCLDIR = include
LIBDIR = libs
EXTENSIONS = c cpp s
LIBS = -lgcc -lmonochrome -lfx
WFLAGS = -Wall
CFLAGS = -I $(INCLDIR) $(WFLAGS)
LFLAGS = -m3 -mb -ffreestanding -nostdlib -T addin.ld -L $(LIBDIR) $(LIBS) -O2 -fno-exceptions
SRCS := $(SRCS) $(foreach EXT,$(EXTENSIONS),$(wildcard $(SRCDIR)/*.$(EXT)))
OBJS := $(OBJS) $(foreach EXT,$(EXTENSIONS),$(patsubst $(SRCDIR)/%.$(EXT),%.o,$(wildcard $(SRCDIR)/*.$(EXT))))
OUT = addin
all : $(OUT).elf
$(OUT).elf : $(OBJS)
$(CC) $(LFLAGS) -o $# $^
$(OBJS) : $(SRCDIR)/$(SRCS)
$(CC) $(CFLAGS) -c $(SRCS)
clean:
rm -f *.o
cleaner:
rm -f *.o $(OUT).elf $(OUT).g1a $(OUT).bin
Generated lines from makefile :
sh3eb-elf-gcc -I include -Wall -c src/MonochromeLib.c src/BTKOM.cpp src/bluetooth.cpp src/syscall.s src/crt0.s
sh3eb-elf-gcc -m3 -mb -ffreestanding -nostdlib -T addin.ld -L libs -lgcc -lmonochrome -lfx -O2 -fno-exceptions -o addin.elf MonochromeLib.o BTKOM.o bluetooth.o syscall.o crt0.o
Output :
BTKOM.o: In function `_main':
BTKOM.cpp:(.text+0xc4): undefined reference to `_memset'
BTKOM.cpp:(.text+0xec): undefined reference to `_GetKey'
bluetooth.o: In function `Bluetooth::Bluetooth()':
bluetooth.cpp:(.text+0xa0): undefined reference to `_srand'
bluetooth.cpp:(.text+0xa4): undefined reference to `_rand'
bluetooth.cpp:(.text+0xac): undefined reference to `_memcpy'
...
There's a reason the built-in linking rule is defined as
$(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $#
LINK.o is
$(CC) $(LDFLAGS) $(TARGET_ARCH)
You should find it works by rewriting your makefile as
LDLIBS := -lgcc -lmonochrome -lfx
LDFLAGS := -nostdlib -T addin.ld -L libs
$(OUT).elf: $(OBJS)
$(LINK.o) $^ $(LDLIBS) -o $#
Note that -O2, ffreestanding and -fno-exceptions are compilation options, not linking options (and I think -m3 and -mb are as well).

My c++ application fails to link, and i don't know why

I am currently getting back in to c++. I have been running into a problem building my application.
When i run make the output becomes:
g++ -c -Wall -I headers/ -I ../libs/inc/SDL2 -L ../libs/lib/SDL2 -L/usr/local/lib -Wl,-rpath,/usr/local/lib -lSDL2 -lGL -lSDL2 -lSDL2main Debug.cpp -o Debug.o
g++ -c -Wall -I headers/ -I ../libs/inc/SDL2 -L ../libs/lib/SDL2 -L/usr/local/lib -Wl,-rpath,/usr/local/lib -lSDL2 -lGL -lSDL2 -lSDL2main GameLoop.cpp -o GameLoop.o
g++ -c -Wall -I headers/ -I ../libs/inc/SDL2 -L ../libs/lib/SDL2 -L/usr/local/lib -Wl,-rpath,/usr/local/lib -lSDL2 -lGL -lSDL2 -lSDL2main Main.cpp -o Main.o
g++ -c -Wall -I headers/ -I ../libs/inc/SDL2 -L ../libs/lib/SDL2 -L/usr/local/lib -Wl,-rpath,/usr/local/lib -lSDL2 -lGL -lSDL2 -lSDL2main -o GAME Debug.o GameLoop.o Main.o
g++: warning: Debug.o: linker input file unused because linking not done
g++: warning: GameLoop.o: linker input file unused because linking not done
g++: warning: Main.o: linker input file unused because linking not done
The file structure of my project
Makefile
Main.cpp
GameLoop.cpp
Debug.cpp
headers/
Main.h
GameLoop.h
Debug.h
Makefile:
CC := g++
TARGET := GAME
SOURCES := $(wildcard *.cpp)
OBJECTS := $(patsubst %.cpp, %.o, $(SOURCES))
DEPS := $(wildcard headers/*.h)
CPPFLAGS := -I ../libs/inc/SDL2 -L ../libs/lib/SDL2 $(shell sdl2-config --libs) -lGL -lSDL2 -lSDL2main
CFLAGS := -c -Wall -I headers/
##$(info OBJECTS= $(OBJECTS) :: SOURCES= $(SOURCES) :: EXECUTABLEOUT= $(TARGET))
default: $(TARGET)
%.o: %.cpp $(DEPS)
$(CC) $(CFLAGS) $(CPPFLAGS) $< -c -o $#
$(TARGET): $(OBJECTS)
$(CC) $(CFLAGS) $(CPPFLAGS) -o $# $^
RAW Paste Data
CC := g++
TARGET := GAME
SOURCES := $(wildcard *.cpp)
OBJECTS := $(patsubst %.cpp, %.o, $(SOURCES))
DEPS := $(wildcard headers/*.h)
CPPFLAGS := -I ../libs/inc/SDL2 -L ../libs/lib/SDL2 $(shell sdl2-config --libs) -lGL -lSDL2 -lSDL2main
CFLAGS := -c -Wall -I headers/
##$(info OBJECTS= $(OBJECTS) :: SOURCES= $(SOURCES) :: EXECUTABLEOUT= $(TARGET))
default: $(TARGET)
%.o: %.cpp $(DEPS)
$(CC) $(CFLAGS) $(CPPFLAGS) $< -c -o $#
$(TARGET): $(OBJECTS)
$(CC) $(CFLAGS) $(CPPFLAGS) -o $# $^
Currently been looking at over 60 answers on this page (and others) with no luck. :/
All your g++ calls use the -c option. From GCC's help:
-c Compile and assemble, but do not link
Your last g++ call should not have the -c option. The reason you have it is because you have specified it for all your CFLAGS:
CFLAGS := -c -Wall -I headers/
Removing it from there should fix your problem.
Remove the -c flag from the last line where you make the executable.
From Man page for g++:
-c Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.

OpenCL/OpenGL Interop on ARM

i have a small problem with OpenCL/OpenGL interoperability in my code
here's my makefile
LIBS = -lm -lOpenCL -lGAL -lGLEW -lglut -lGLU -lpthread
CFLAGS = -Wall -g
OBJECTS = main.o environment.o input.o animate.o buddhabrot.o buddhacl.o cmodules/timer.o
all: prognonmpi
prognonmpi: $(OBJECTS)
LIBRARY_PATH=/usr/lib/arm-linux-gnueabi/ c++ $(CFLAGS) -o prognonmpi $(OBJECTS) $(LIBS)
%.o: %.cpp $(LIBS)
clean:
rm -f *.o prog cmodules/*.o
the code just comiles fine, but i get a linker error with the following message
LIBRARY_PATH=/usr/lib/arm-linux-gnueabi/ c++ -Wall -g -o prognonmpi main.o environment.o input.o animate.o buddhabrot.o buddhacl.o cmodules/timer.o -lm -lOpenCL -lGAL -lGLEW -lglut -lGLU -lpthread
main.o: In function `displayFunc()':
main.cpp:(.text+0xdda): undefined reference to `clEnqueueAcquireGLObjects'
main.cpp:(.text+0xe12): undefined reference to `clEnqueueReleaseGLObjects'
main.o: In function `initOpenGLBuffers(int, int)':
main.cpp:(.text+0x136a): undefined reference to `clCreateFromGLBuffer'
collect2: ld returned 1 exit status
make: *** [prognonmpi] Error 1
the code compiles very fine on my notebook with the lins, using -lGL instead of -lGAL, seesm -lGAL is ARM specific.
on the ARM machine i get the above mentioned error.
Any suggestions what is missing on my ARM-machine?
using Ubuntu as OS

Can't get GLFW to link

I am trying to compile the below test program:
#include <GL/glfw.h>
int main(int argc, char** argv) {
if(!glfwInit()) {
return -1;
}
if(!glfwOpenWindow(640, 480, 8, 8, 8, 0, 24, 0, GLFW_WINDOW)) {
return -1;
}
while(glfwGetWindowParam(GLFW_OPENED)) {
glfwSwapBuffers();
}
return 0;
}
but I always get undefined reference errors in regards to the GLFW functions.
Below is my makefile:
CXX = clang++
CXXFLAGS = -Wall -std=c++0x
LDFLAGS = -lglfw
OBJ_DIR = bin
LIB_DIR = -L/usr/lib
INC_DIR = -I/usr/include
SOURCE = main.cpp
OBJECTS = ${SOURCE:%.cpp=$(OBJ_DIR)/%.o}
EXECUTABLE = hello
all: init $(OBJECTS) $(EXECUTABLE)
$(EXECUTABLE):
$(CXX) $(CXXFLAGS) $(LDFLAGS) $(LIB_DIR) -o $# $(OBJECTS)
$(OBJ_DIR)/%.o: %.cpp
$(CXX) $(INC_DIR) -c $< -o $#
init:
#mkdir -p "$(OBJ_DIR)"
clean:
#rm -rf $(OBJ_DIR) $(EXECUTABLE)
I definitely have glfw.h and libglfw.a/.so as when I run locate glfw I get:
:~$ locate glfw
/usr/include/GL/glfw.h
/usr/lib/libglfw.a
/usr/lib/libglfw.so
/usr/lib/libglfw.so.2
/usr/lib/libglfw.so.2.6
The output of nm /usr/lib/libglfw.a | grep glfwInit:
:~$ nm /usr/lib/libglfw.a | grep glfwInit
U _glfwInitialized
U _glfwInitialized
U _glfwInitialized
U _glfwInitialized
0000000000000000 B _glfwInitialized
0000000000000000 T glfwInit
U _glfwInitialized
U _glfwInitialized
U _glfwInitialized
U _glfwInitialized
U _glfwInitialized
U _glfwInitJoysticks
U _glfwInitTimer
00000000000000c0 T _glfwInitJoysticks
0000000000000000 T _glfwInitTimer
and the verbose message from clang:
clang++ -I/usr/include -c main.cpp -o bin/main.o
clang++ -Wall -std=c++0x -Wl --verbose -lglfw -lGL -lGLU -L/usr/lib -o hello bin/main.o
Ubuntu clang version 3.0-6ubuntu3 (tags/RELEASE_30/final) (based on LLVM 3.0)
Target: x86_64-pc-linux-gnu
Thread model: posix
clang: warning: argument unused during compilation: '-std=c++0x'
"/usr/bin/ld" -z relro --hash-style=gnu --as-needed --build-id --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o hello /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.6/crtbegin.o -L/usr/lib -L/usr/lib/gcc/x86_64-linux-gnu/4.6 -L/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.6/../../.. -L/lib/x86_64-linux-gnu -L/lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib -lglfw -lGL -lGLU bin/main.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-linux-gnu/4.6/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crtn.o
bin/main.o: In function `main':
main.cpp:(.text+0x17): undefined reference to `glfwInit'
main.cpp:(.text+0x76): undefined reference to `glfwOpenWindow'
main.cpp:(.text+0x97): undefined reference to `glfwGetWindowParam'
main.cpp:(.text+0xa7): undefined reference to `glfwSwapBuffers'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [hello] Error 1
It seems to not be finding the library?
The problem is that the glfw libraries are being specified to the linker before the object file that depends on them. ld searches libraries to resolve dependencies only for the dependencies that it knows about at the point in list of files it's processing. So when ld is searching libglfw.a it doesn't know about the glfwInit dependency in main.o yet. ld (by default) doesn't go back an search the library again.
Try:
$(EXECUTABLE):
$(CXX) $(CXXFLAGS) $(LIB_DIR) -o $# $(OBJECTS) $(LDFLAGS)
Also the libraries should probably be specified in a LDLIBS (or LIBS) variable - LDFLAGS is conventionally use for linker options:
CXX = clang++
CXXFLAGS = -Wall -std=c++0x
LDLIBS = -lglfw -lGL -lGLU
OBJ_DIR = bin
LIB_DIR = -L/usr/lib
INC_DIR = -I/usr/include
SOURCE = main.cpp
OBJECTS = ${SOURCE:%.cpp=$(OBJ_DIR)/%.o}
EXECUTABLE = hello
all: init $(OBJECTS) $(EXECUTABLE)
$(EXECUTABLE):
$(CXX) $(LDFLAGS) $(LIB_DIR) -o $# $(OBJECTS) $(LDLIBS)
$(OBJ_DIR)/%.o: %.cpp
$(CXX) $(INC_DIR) -c $< -o $#
init:
#mkdir -p "$(OBJ_DIR)"
clean:
#rm -rf $(OBJ_DIR) $(EXECUTABLE)