Errors Linking ColladaDom - gcc

I'm trying to build a tool that uses colladadom, but I'm getting linker errors. This is my makefile
CXXFLAGS = -Wall -fPIC -g -t
OBJECTS = mesher.o oar.o primitive.o assigner.o meshmerizer.o primmesher.o colladifier.o main.o
LIBS = -I/opt/local/include -L/opt/local/lib -lboost_system -lboost_filesystem - lcollada14dom
default: all
all: $(OBJECTS)
$(CXX) $(CXXFLAGS) -o oarcoll $^ $(LIBS)
main.o : src/meshmerizer.h src/primmesher.h src/main.cpp
$(CXX) $(CXXFLAGS) -c $^
colladifier.o : src/colladifier.cpp
$(CXX) $(CXXFLAGS) -c $^
assigner.o : lib/assigner.cpp
$(CXX) $(CXXFLAGS) -c $^
primitive.o : lib/assigner.h src/utils.h src/prim_enum.h src/primitive.cpp
$(CXX) $(CXXFLAGS) -c $^
mesher.o : src/mesher.cpp
$(CXX) $(CXXFLAGS) -c $^
oar.o : src/oar.cpp
$(CXX) $(CXXFLAGS) -c $^
primmesher.o : src/primmesher.cpp
$(CXX) $(CXXFLAGS) -c $^
meshmerizer.o : src/meshmerizer.cpp src/primmesher.h
$(CXX) $(CXXFLAGS) -c $^
clean:
rm -f oarcoll main.o $(OBJECTS) *~
When I compile, I get this result:
Undefined symbols for architecture x86_64:
"boost::system::generic_category()", referenced from:
__static_initialization_and_destruction_0(int, int)in colladifier.o
__static_initialization_and_destruction_0(int, int)in main.o
"boost::system::system_category()", referenced from:
__static_initialization_and_destruction_0(int, int)in colladifier.o
__static_initialization_and_destruction_0(int, int)in main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [all] Error 1
I can compile with no problem if I do it all in one line and do not make .o files and link them later. I.e. this:
g++ domTest.cc -I/opt/local/include -L/opt/local/lib -lboost_system -lcollada15dom -o domTest
works fine on the same machine.
I've run nm on libboost_system.dylib and the symbols that are undefined appear in the dump.
There are multiple other threads similar to this, I've seen them and tried the solutions, but it still does not work for me.
Any ideas?

Related

No such file or directory g++ Include Directory

I am new to C++ programming and making MakeFiles, so straight to the point:
here is my MakeFile:
CC = g++
INCLUDES = -I\ include/
CFLAGS = -Wall \ -g\ $(INCLUDES)
LDFLAGS = -L./lib
main: main.o
$(CC) main.o $(LDFLAGS) -o main
main.o: src/main.cpp
$(CC) $(CFLAGS) -c src/main.cpp -o main.o
Sorry for noobness :( . my File structure:
src/
include/
Makefile
and "make" shows :
g++: error: -g -I include/: No such file or directory
Kindly please also give suggestions for improving this file
You dont require \ after every option
Please see below changes:
CC = g++
INCLUDES = include/
CFLAGS = -Wall -g -I$(INCLUDES)
LDFLAGS = -L./lib
main: main.o
$(CC) main.o $(LDFLAGS) -o main
main.o: src/main.cpp
$(CC) $(CFLAGS) -c src/main.cpp -o main.o

Link dylib library

I'm trying to link a dylib to my makefile on Mac, but Clang gives this message:
Undefined symbols for architecture x86_64:
"_zbesj_wrap", referenced from:
sp_bessel::besselJ(double, std::__1::complex<double>) in besselJ.o
"_zbesy_wrap", referenced from:
sp_bessel::besselJ(double, std::__1::complex<double>) in besselJ.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I installed the library in /usr/lib, where I can see libcomplex_bessel.0.6.0.dylib and libcomplex_bessel.dylib.
This is my makefile:
OBJS = main.o besselJ.o
CC = c++
CFLAGS = -std=c++11 -stdlib=libc++
LIBS = -L/usr/lib -lcomplex_bessel
PROGRAM_NAME = test
all: $(PROGRAM_NAME)
$(PROGRAM_NAME): $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $#
main.o: main.cpp
$(CC) $(CFLAGS) -c $< -o $#
besselJ.o: besselJ.cpp
$(CC) $(CFLAGS) -c $< -o $# $(LIBS)
After reading other questions, I tried different combinations for -L and -l but nothing worked. Sorry but it's my first time with external libraries...
I changed my makefile to this:
OBJS = main.o besselJ.o
CC = c++
CFLAGS = -std=c++11 -stdlib=libc++
LIBS = -L/usr/lib -lcomplex_bessel
PROGRAM_NAME = test
all: $(PROGRAM_NAME)
$(PROGRAM_NAME): $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $# $(LIBS)
main.o: main.cpp
$(CC) $(CFLAGS) -c $< -o $#
besselJ.o: besselJ.cpp
$(CC) $(CFLAGS) -c $< -o $#
but I still have problems, I get this message:
c++ -std=c++11 -stdlib=libc++ main.o besselJ.o -o test -L/usr/lib -lcomplex_bessel
ld: library not found for -lcomplex_bessel
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [test] Error 1
Ok, I think I solved it using xcode-select --install: other users had the same problem with libraries after updating it.
You need to pass libs to linker, not to the compiler:
OBJS = main.o besselJ.o
CC = c++
CFLAGS = -std=c++11 -stdlib=libc++
LIBS = -L/usr/lib -lcomplex_bessel
PROGRAM_NAME = test
all: $(PROGRAM_NAME)
$(PROGRAM_NAME): $(OBJS)
$(CC) $(OBJS) -o $# $(LIBS)
main.o: main.cpp
$(CC) $(CFLAGS) -c $< -o $#
besselJ.o: besselJ.cpp
$(CC) $(CFLAGS) -c $< -o $#

Compiling with nvcc and g++

I'm using Nvidia's nvcc compiler to compile a .cpp file which will contain device code (but only contains host code now). The program I am using is written in C and C++ and uses gcc/g++ to compile with and g++ is used to link the final executable together. Originally, when I run my program using -Ofast, it runs in about 2 seconds, but I factored in nvcc to compile 1 .cpp file and the rest with gcc/g++, have g++ link it together, the program takes about 8 seconds to run. If I compile more .cpp files with nvcc, it slows down even more (~10 seconds). I tried adjusting my make file to compile everything with nvcc but I received too many errors from the C code. Only the C++ code seems to be able to run without errors when compiled with nvcc. Does nvcc not optimise the host code? Why does this happen? What is the best way to structure the program?
Makefile
CC = gcc
CCPP = g++
NVCC = /vol/cuda/8.0.61/bin/nvcc
#preprocessor defines
DEFINES = -DUSE_ZLIB
OPTFLAGS = -Ofast
#CFLAGS = -ansi -pedantic -Wall -Werror -fPIC
#CFLAGS = -ansi -pedantic -Wall -fPIC
CFLAGS = -ansi $(OPTFLAGS)
NVFLAGS = -x c++ -Wno-deprecated-gpu-targets -ccbin g++
NVLINKFLAGS = -L/vol/cuda/8.0.61/lib64/ -lcuda -lcudart
#NVLINKFLAGS += -L/vol/cuda/8.0.61/lib -lcuda -lcudart
CPPFLAGS = $(OPTFLAGS) -I./include $(DEFINES)
LINKFLAGS = -lz
all : simple
objs = src/tourtre.o \
src/ctArc.o \
src/ctBranch.o \
src/ctComponent.o \
src/ctNode.o \
src/ctQueue.o \
src/ctNodeMap.o \
examples/simple/obj/Data.o \
examples/simple/obj/Mesh.o \
examples/simple/obj/main.o
simple : $(objs)
$(CCPP) $(CPPFLAGS) -o simple $^ $(LINKFLAGS) $(NVLINKFLAGS)
src/tourtre.o : src/tourtre.c include/tourtre.h src/ctMisc.h include/ctArc.h include/ctNode.h src/ctComponent.h include/ctNode.h src/ctQueue.h src/ctAlloc.h
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $#
src/ctArc.o : src/ctArc.c include/tourtre.h src/ctMisc.h include/ctArc.h
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $#
src/ctBranch.o : src/ctBranch.c include/tourtre.h src/ctMisc.h include/ctBranch.h
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $#
src/ctComponent.o : src/ctComponent.c include/tourtre.h src/ctMisc.h src/ctComponent.h
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $#
src/ctNode.o : src/ctNode.c include/tourtre.h src/ctMisc.h include/ctNode.h
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $#
src/ctQueue.o : src/ctQueue.c include/tourtre.h src/ctMisc.h src/ctQueue.h
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $#
src/ctNodeMap.o : src/ctNodeMap.c src/ctNodeMap.h include/ctNode.h src/ctQueue.h src/sglib.h
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $#
examples/simple/obj/Data.o: examples/simple/src/Data.cpp examples/simple/src/Data.h examples/simple/src/Global.h
$(CCPP) $(CPPFLAGS) $(CFLAGS) -c $< -o $#
examples/simple/obj/Mesh.o: examples/simple/src/Mesh.cpp examples/simple/src/Data.h examples/simple/src/Global.h examples/simple/src/Mesh.h
$(NVCC) $(NVFLAGS) -c $< -o $#
examples/simple/obj/main.o: examples/simple/src/main.cpp examples/simple/src/Data.h examples/simple/src/Global.h examples/simple/src/Mesh.h
$(CCPP) $(CPPFLAGS) $(CFLAGS) -c $< -o $#
clean :
-rm -rf src/*.o examples/simple/obj/*.o doc/html
Add -Xcompiler -ansi -Xcompiler -Ofast to NVFLAGS so that the host compiler gets the same (optimization) flags from nvcc as when run directly.

CFLAGS are ignored in Makefile

I am using the following makefile to build my project:
CC = /usr/bin/g++
CFLAGS = -Wall -pedantic -std=c++0x
LDFLAGS =
OBJ = main.o pnmhandler.o pixmap.o color.o
pnm: $(OBJ)
$(CC) $(CFLAGS) -o pnm $(OBJ) $(LDFLAGS)
%.o: %.c
$(CC) $(CFLAGS) -c $<
As I run make I get the following error:
/usr/include/c++/4.9.1/bits/c++0x_warning.h:32:2: error: #error This
file requires compiler and library support for the ISO C++ 2011
standard. This support is currently experimental, and must be enabled
with the -std=c++11 or -std=gnu++11 compiler options.
As I can read from the following line, the CFLAGS are not properly included, but I have no idea what I am doing wrong:
g++ -c -o main.o main.cpp
Also tried -std=c++11 and -std=gnu++11, without any results. Any ideas?
If I run make -Bn, I get:
g++ -c -o main.o main.cpp
g++ -c -o pnmhandler.o pnmhandler.cpp
g++ -c -o pixmap.o pixmap.cpp
g++ -c -o color.o color.cpp
/usr/bin/g++ -Wall -pedantic -std=c++0x -o pnm main.o pnmhandler.o pixmap.o color.o
EDIT: Replacing the rule %.o: %.c with %.o: %.cpp fixes my problem.
The reason you see
g++ -c -o main.o main.cpp
is that Make is invoking its standard rule to create the object file:
%.o: %.cpp
# recipe to execute (built-in):
$(COMPILE.cpp) $(OUTPUT_OPTION) $<
The command expands to
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $# $<
Instead of setting CC and CFLAGS in your makefile, you should set CXX and CXXFLAGS, which are meant for C++ rather than C. That allows the built-in rule above to work for you, and then you just need to make sure the right linker is used, e.g. with
pnm: LINK.o=$(LINK.cc)
pnm: $(OBJ)
You also don't need the %.o: %.c rule, as you have no C sources.
Complete Makefile:
CXX = /usr/bin/g++
CXXFLAGS = -Wall -pedantic -std=c++0x
OBJ = main.o pnmhandler.o pixmap.o color.o
pnm: LINK.o=$(LINK.cc)
pnm: $(OBJ)
clean::
$(RM) pnm
.PHONY: clean

Make: error compiling .o

My makefile below works after I clean and remove all object files, however if I edit a source file and then run make again, it outputs the error below. I believe this is coming from the lines:
%.o: %.cpp
$(CXX) -c $(CFLAGS) $< -o $#
I thought I had a basic grasp of automatic variables, but something isn't right.
Makefile:
all: main
CFLAGS=-fPIC -g -Wall `pkg-config --cflags opencv` -I /usr/local/include/libusb-1.0/
LIBS = `pkg-config --libs opencv`
INCLUDE = -I /usr/local/include/libfreenect
FREE_LIBS = -L/usr/local/lib -lfreenect
main: device.cpp cups.cpp main.cpp
$(CXX) $(INCLUDE) $(CFLAGS) $? -o $# $(LIBS) $(FREE_LIBS)
%.o: %.cpp
$(CXX) -c $(CFLAGS) $< -o $#
clean:
rm -rf *.o main
Output from make:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1
In the line
$(CXX) $(INCLUDE) $(CFLAGS) $? -o $# $(LIBS) $(FREE_LIBS)
you use $?, which means only the updated dependencies will be used in the command. (You should be seeing this in the command.)
Use $+ instead. (See the manual.)
(PS: This is one half of the answer; the other half is Sagar Sakre's advice.)
main taget should depend on .o files instead of .cpp
change
main: device.cpp cups.cpp main.cpp
to
main: device.o cups.o main.o
everything should work fine

Resources