I'm having linking or compilation errors - c++

I am using Netbeans for my C++ project. I compiled my program using make and ran into this error:
collect2: error: ld terminated with signal 11 [Segmentation fault], core dumped
Makefile:4: recipe for target 'barn' failed
make: *** [barn] Error 1
Whereas, when I compiled it in a linux environment(Ubuntu to be precise), it compiled fine. What could have possibly gone wrong?
This is what I got when I typed make -n:
g++ -c main.cc
g++ -c Animal.cc
g++ -c Bird.cc
g++ -c Chicken.cc
g++ -c Cat.cc
g++ -c Pig.cc
g++ -o barn main.o Animal.o Bird.o Chicken.o Cat.o Pig.o Random.o
PS I prefer using Netbeans

A segment fault in the linker suggests a bug with that. This is what I would try if I were to run into this problem.
At the command line do
g++ -o barn main.cc Animal.cc Bird.cc Chicken.cc Cat.cc Pig.cc Random.cc
If that does not work, try variations like:
g++ -o barn main.cc Pig.cc Random.cc Animal.cc Bird.cc Chicken.cc Cat.cc
The order should not matter. This is just the kind of thing I would try with a mystery-meat problem like this.

Related

Segmentation fault when cross compiling Windows compatible .exe from Linux development env

I use a Linux environment for code development and testing of a program and now I want to compile an Windows ".exe" file so that my program can run on Windows OS, after some internet research I tried to modified my makefile to use x86_64-w64-mingw32-g++ instead of g++ as its compiler but now I get a segmentation fault at the end of the compilation and I am not sure how to debug this. My makefile is below:
PROGRAM=output.exe
COMPILER=x86_64-w64-mingw32-g++
# PROGRAM=output
# COMPILER=g++
#BIN_FOLDER=bin
DEPS += Defs.h Structs.h
CXXFLAGS+=`sdl2-config --cflags`
CXXFLAGS+=-DVERSION=$(VERSION) -DREVISION=$(REVISION) -DDATA_DIR=\"$(DATA_DIR)\"
CXXFLAGS+=-Wall -Wempty-body -Werror -Werror=maybe-uninitialized -Warray-bounds
CXXFLAGS+=-g -lefence -std=c++20 -fopenmp
LDFLAGS+=-L/usr/local/lib -lSDL2 -lSDL2_mixer -lSDL2_image -lSDL2_ttf -fopenmp src/ECS/*.cpp #-pg
FILENAME:= $(patsubst %.cpp,%.o,$(wildcard src/*.cpp))
$(PROGRAM):$(FILENAME)
$(COMPILER) $(FILENAME) -o $# $(LDFLAGS)
clean:
rm src/*.o
this compiles without a problem in g++ but when using the mingw32 compiler I get this error:
x86_64-w64-mingw32-g++ src/ActionManager.o src/AStar.o ...[a bunch of .o files] -o output.exe
collect2: fatal error: ld terminated with signal 11 [Segmentation fault], core dumped
compilation terminated.
make: *** [makefile:26: output.exe] Error 1
I tried compiling a simple helloWorld.cpp program using x86_64-w64-mingw32-g++ like this: x86_64-w64-mingw32-g++ 01_hello_world.c -o windowshello.exe and that works fine.
I also tried deleting all my LDFLAGS just to see what would happen and I get the same result.
Could anyone point me into the right direction as to how I might debug this? Is this a particular part of my code that is causing this or am I going about this entirely wrong?

Compiling Dynamically Linked Library in C++ with Run Path Issue

I'm busy trying to compile and link a c++ program using the following make file
driver.so:driver.cpp
g++ -c driver.cpp -o driver.so
g++ -L/tokenlib/libtokenlib.so driver.so -o linked
but I'm getting the error
g++ -c driver.cpp -o driver.so
g++ -L/tokenlib/libtokenlib.so driver.so -o linked
driver.so: In function `main':
driver.cpp:(.text+0x9): undefined reference to `tokenlib::acquire_token()'
collect2: error: ld returned 1 exit status
make: *** [driver.so] Error 1
and my file hierarchy is
/root
makefile
driver.cpp
/tokenlib
libtokenlib.so
I've been told that adding -Wl,-rpath=./tokenlib to my g++ arguements would solve the problem so I tried like this
driver.so:driver.cpp
g++ -c driver.cpp -o driver.so -Wl,-rpath=./tokenlib
g++ -L/tokenlib/libtokenlib.so driver.so -o linked -Wl,-rpath=./tokenlib
but it still isn't working.
I'm a complete novice and any help would be appreciated.
The linker usually refers to .a (stub) libraries, not to .so files!
-L options specify the paths, the lnker should take in account.
The -l<mystuff> option tries to resolve a library named libmystuff.a from any of the given path's of the -L options, or compiler intrinsic library search paths.

Error 1 while buiding by G++

I'm using eclipse Junior and build my C++ program.
the command line in the console is as following:
make all
Building file: ../src/misc/bt_misc.cpp
Invoking: GCC C++ Compiler
g++ -DNDEBUG=1 -DEPOLL=1 -D__GXX_EXPERIMENTAL_CXX0X__
-I"/home/adtech/workspace/mtracker/src/misc" -I"/home/adtech/workspace/mtracker/src/Tracker" -I"/home/adtech/workspace/mtracker/src/misc/xbt" -I/usr/include -I/usr/lib64/mysql -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -MMD -MP -MF"src/misc/bt_misc.d" -MT"src/misc/bt_misc.d" -o "src/misc/bt_misc.o" "../src/misc/bt_misc.cpp"
I got no error during the compilation phase, unfortunately, encouter an error at the end as following:
make: *** [src/Tracker/server.o] Error 1
Could you guys help me explain this error. Thanks so much
That error means that a command it executed (presumably g++ when building server.o) en­count­er­ed an error. Scroll up and make sure there aren't any errors. The error might not be at the very end if you're running multiple jobs in parallel.

Cannot link LIBEVENT as C++

Why this does not work, file test.c:
#include <event.h>
int main(void)
{
event_init();
return 0;
}
Then:
gcc -o test.o -c test.c runs OK, but
Link:
g++ -o test -levent test.o produces
test.o: In function `main':
test.c:(.text+0x5): undefined reference to `event_init'
collect2: ld returned 1 exit status
So it cannot be linked as C++. How to solve this? I need to link it as C++ and compile as C.
This question has been asked many times. On Linux, you should put libraries after object and source files in the compilation command. So try
g++ -Wall -g -c mytest.cc
g++ -Wall -g mytest.o -levent -o mytest
Avoid calling your test program test which is an existing utility or shell builtin.
As a newbie, remember to always compile with all warnings asked -Wall and for debugging -g and learn to use gdb

Using exit() in c++

For one reason or another, I am messing around with the exit() function in c++. I am getting all kinds of strange errors from my mac running lion (64 bit). I am compiling using g++ -o -g -Wall.
Exhibit A:
#include <iostream>
int main(int arc, char *argv[]){
exit(1);
}
The Terminal output looks like this
$ g++ -o -g -Wall test main.cpp
ld: in test, can't link with a main executable for architecture x86_64
collect2: ld returned 1 exit status
but $ g++ -o test main.cpp compiles fine.
using #include<stdio.h> or #include<stdlib.h> result in the same compilation error.
I am just wondering if anyone might be able to see immediately what is going on here?
test is the name of the binary to produce, your first argument list should be:
> g++ -g -Wall -o test main.cpp
^^^^^^^ -o has test for an argument
-o is meant to be followed immediately by the name of the output file. It is probably trying to use your old binary 'test' as a source file, incorrectly.
Try this:
g++ -o test -g -Wall main.cpp