Modifying CUDA and cpp Makefile - c++

I am trying to modify my makefile to support .cpp and .cu, however, I keep getting an error such as:
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o(.text+0x20): error: undefined reference to 'main'
hostCode.o:displayfunc.cpp:function readScene(char*): error: undefined reference to 'camera'
hostCode.o:displayfunc.cpp:function readScene(char*): error: undefined reference to 'camera'
hostCode.o:displayfunc.cpp:function readScene(char*): error: undefined reference to 'camera'
hostCode.o:displayfunc.cpp:function readScene(char*): error: undefined reference to 'camera'
hostCode.o:displayfunc.cpp:function readScene(char*): error: undefined reference to 'sphereCount'
hostCode.o:displayfunc.cpp:function readScene(char*): error: undefined reference to 'sphereCount'
hostCode.o:displayfunc.cpp:function readScene(char*): error: undefined reference to 'sphereCount'
hostCode.o:displayfunc.cpp:function readScene(char*): error: undefined reference to 'spheres'
hostCode.o:displayfunc.cpp:function readScene(char*): error: undefined reference to 'spheres'
hostCode.o:displayfunc.cpp:function readScene(char*): error: undefined reference to 'sphereCount'
hostCode.o:displayfunc.cpp:function idleFunc(): error: undefined reference to 'updateRendering()'
hostCode.o:displayfunc.cpp:function reshapeFunc(int, int): error: undefined reference to 'reInitCamera(bool)'
hostCode.o:displayfunc.cpp:function keyFunc(unsigned char, int, int): error: undefined reference to 'reInitCamera(bool)'
Makefile
CXX = g++
NVCC = nvcc -ccbin $(CXX)
INCLUDES := -I/home/cuda_app/inc/
LDFLAGS = -lGL -lGLU -lglut -lpthread
ALL:= test
test: hostCode.o deviceCode.o
$(NVCC) $(INCLUDES) -o $# $< $(LDFLAGS)
deviceCode.o: SmallPtCUDA.cu
$(NVCC) $(INCLUDES) -o $# -c $< $(LDFLAGS)
hostCode.o: displayfunc.cpp
$(CXX) $(INCLUDES) -o $# -c $< $(LDFLAGS)
clean:
rm -rf *.o $(ALL)
How can I compile both .cpp and .cu?
Can anybody help with this please? Thanks

When asking a question about build errors, it's always best to include not just the errors but also the command invoking the compiler or linker. With errors like this the problem is there; seeing the error messages is helpful but not definitive.
In your case the problem is your link command is incorrect in your makefile:
test: hostCode.o deviceCode.o
$(NVCC) $(INCLUDES) -o $# $< $(LDFLAGS)
You are using $< here which expands to just the first prerequisite. If you'd included the link line and examined it you'd see that the deviceCode.o file is not present on the link line. You want your link command to look like this:
test: hostCode.o deviceCode.o
$(NVCC) $(INCLUDES) -o $# $^ $(LDFLAGS)
Using $^ which expands to all the prerequisites.
Also, just a note that by convention LDFLAGS contains linker options like -L, and LDLIBS is used for linker options like -l. However your setup will work (it's just not conventional).

Related

undefined reference to sqrt in SVM-Struct (have used -lm)

Actually, I am working with the well-known SVM-struct project (http://www.cs.cornell.edu/people/tj/svm_light/svm_struct.html)on Ubuntu 16.04. I followed the instructions in http://www.cs.cornell.edu/people/tj/svm_light/svm_multiclass.html to use SVM-multiclass, downloaded the source code and make. But I met some errors when building the project:
$ make
cd svm_light; make svm_learn_hideo_noexe
make[1]: Entering directory '/home/parallels/CLionProjects/svm_multiclass/svm_light'
make[1]: Nothing to be done for 'svm_learn_hideo_noexe'.
make[1]: Leaving directory '/home/parallels/CLionProjects/svm_multiclass/svm_light'
cd svm_struct; make svm_struct_noexe
make[1]: Entering directory '/home/parallels/CLionProjects/svm_multiclass/svm_struct'
make[1]: Nothing to be done for 'svm_struct_noexe'.
make[1]: Leaving directory '/home/parallels/CLionProjects/svm_multiclass/svm_struct'
gcc -O3 -lm -Wall svm_struct/svm_struct_learn.o svm_struct_learn_custom.o svm_struct_api.o svm_light/svm_hideo.o svm_light/svm_learn.o svm_light/svm_common.o svm_struct/svm_struct_common.o svm_struct/svm_struct_main.o -o svm_multiclass_learn
svm_light/svm_learn.o: In function `estimate_sphere':
svm_learn.c:(.text+0x6e88): undefined reference to `sqrt'
svm_light/svm_learn.o: In function `estimate_r_delta':
svm_learn.c:(.text+0x7053): undefined reference to `sqrt'
svm_light/svm_learn.o: In function `estimate_r_delta_average':
svm_learn.c:(.text+0x7b04): undefined reference to `sqrt'
svm_light/svm_learn.o: In function `length_of_longest_document_vector':
svm_learn.c:(.text+0x7b86): undefined reference to `sqrt'
svm_light/svm_learn.o: In function `incorporate_unlabeled_examples':
svm_learn.c:(.text+0x89eb): undefined reference to `sqrt'
svm_light/svm_learn.o:svm_learn.c:(.text+0xc952): more undefined references to `sqrt' follow
svm_light/svm_common.o: In function `classify_example':
svm_common.c:(.text+0x38f): undefined reference to `tanh'
svm_common.c:(.text+0x42d): undefined reference to `exp'
svm_common.c:(.text+0x4a7): undefined reference to `pow'
svm_light/svm_common.o: In function `kernel':
svm_common.c:(.text+0x870): undefined reference to `tanh'
svm_common.c:(.text+0x90d): undefined reference to `exp'
svm_common.c:(.text+0x988): undefined reference to `pow'
svm_light/svm_common.o: In function `model_length_s':
svm_common.c:(.text+0x2c8d): undefined reference to `sqrt'
svm_light/svm_common.o: In function `model_length_n':
svm_common.c:(.text+0x2f5c): undefined reference to `sqrt'
svm_light/svm_common.o: In function `cholesky_matrix':
svm_common.c:(.text+0x3c6f): undefined reference to `sqrt'
svm_light/svm_common.o: In function `find_indep_subset_of_matrix':
svm_common.c:(.text+0x3f41): undefined reference to `sqrt'
svm_light/svm_common.o: In function `single_kernel':
svm_common.c:(.text+0xc0c): undefined reference to `tanh'
svm_common.c:(.text+0xc84): undefined reference to `pow'
svm_common.c:(.text+0xcfb): undefined reference to `exp'
collect2: error: ld returned 1 exit status
Makefile:48: recipe for target 'svm_multiclass_learn' failed
make: *** [svm_multiclass_learn] Error 1
I think the Makefile has already included -lm.
# Makefile for SVM-multiclass, 03.07.04
#Use the following to compile under unix or cygwin
CC = gcc
LD = gcc
#Call 'make' using the following line to make CYGWIN produce stand-alone Windows executables
# make 'SFLAGS=-mno-cygwin'
CFLAGS = $(SFLAGS) -O3 -fomit-frame-pointer -ffast-math -Wall
LDFLAGS = $(SFLAGS) -O3 -lm -Wall
#CFLAGS = $(SFLAGS) -pg -Wall
#LDFLAGS = $(SFLAGS) -pg -lm -Wall
all: svm_multiclass_learn svm_multiclass_classify
.PHONY: clean
clean: svm_light_clean svm_struct_clean
rm -f *.o *.tcov *.d core gmon.out *.stackdump
#-----------------------#
#---- SVM-light ----#
#-----------------------#
svm_light_hideo_noexe:
cd svm_light; make svm_learn_hideo_noexe
svm_light_clean:
cd svm_light; make clean
#----------------------#
#---- STRUCT SVM ----#
#----------------------#
svm_struct_noexe:
cd svm_struct; make svm_struct_noexe
svm_struct_clean:
cd svm_struct; make clean
#-------------------------#
#---- SVM MULTICLASS ----#
#-------------------------#
svm_multiclass_classify: svm_light_hideo_noexe svm_struct_noexe svm_struct_api.o svm_struct/svm_struct_classify.o svm_struct/svm_struct_common.o svm_struct/svm_struct_main.o
$(LD) $(LDFLAGS) svm_struct_api.o svm_struct/svm_struct_classify.o svm_light/svm_common.o svm_struct/svm_struct_common.o -o svm_multiclass_classify $(LIBS)
svm_multiclass_learn: svm_light_hideo_noexe svm_struct_noexe svm_struct_api.o svm_struct_learn_custom.o svm_struct/svm_struct_learn.o svm_struct/svm_struct_common.o svm_struct/svm_struct_main.o
$(LD) $(LDFLAGS) svm_struct/svm_struct_learn.o svm_struct_learn_custom.o svm_struct_api.o svm_light/svm_hideo.o svm_light/svm_learn.o svm_light/svm_common.o svm_struct/svm_struct_common.o svm_struct/svm_struct_main.o -o svm_multiclass_learn $(LIBS)
svm_struct_api.o: svm_struct_api.c svm_struct_api.h svm_struct_api_types.h svm_struct/svm_struct_common.h
$(CC) -c $(CFLAGS) svm_struct_api.c -o svm_struct_api.o
svm_struct_learn_custom.o: svm_struct_learn_custom.c svm_struct_api.h svm_light/svm_common.h svm_struct_api_types.h svm_struct/svm_struct_common.h
$(CC) -c $(CFLAGS) svm_struct_learn_custom.c -o svm_struct_learn_custom.o
The linker collects dependencies first (svm_light/svm_learn.o) and fills them in when it sees a definition (-lm).
You seem to already have a $(LIBS) at the end of your ld invocation, so just:
LIBS += -lm
should fix it.
Other tips:
you can replace the pattern cd X; make TARGET with make -C X TARGET
you can refer to the target as $# and the first prerequisite in the dependency list as $<, so your last rule can be written as:
svm_struct_learn_custom.o: svm_struct_learn_custom.c svm_struct_api.h svm_light/svm_common.h svm_struct_api_types.h svm_struct/svm_struct_common.h
$(CC) -c $(CFLAGS) $< -o $#
where the recipe redefinition of the built-in recipe for compiling .c files, so it suffices to simply list the headers as (extra) prerequisites:
svm_struct_learn_custom.o: svm_struct_api.h svm_light/svm_common.h svm_struct_api_types.h svm_struct/svm_struct_common.h

Linker Error when compiling with static library [duplicate]

This question already has answers here:
Why does the order in which libraries are linked sometimes cause errors in GCC?
(9 answers)
Closed 6 years ago.
I have a project with several .cpp and .h files, and I am trying to split the source into core / generic stuff, and application specific code. Based on that, I separated the code files into a Common folder and compiled it as a library. Below is the Makefile for that lib:
CFLAGS=-std=c++11 -g -c -pedantic -Wall -Wextra -I../boost_1_57_0 -L../boost_1_57_0/stage/lib
SOURCES = $(wildcard *.cpp)
OBJECTS=$(SOURCES:.cpp=.o)
OUTPUTFILE=libcommon.a
all: $(OUTPUTFILE)
$(OUTPUTFILE): $(OBJECTS)
ar rcs $(OUTPUTFILE) $(OBJECTS)
.cpp.o:
$(CC) $(CPPFLAGS) $(CFLAGS) $< -o $# $(GPROF)
clean:
rm -f $(OBJECTS) $(OUTPUTFILE)
Then I created a sample main file just to try out the compilation of an app using that library.
Here is the code of the main source file:
#include "../Common/Functions.h"
#include "../Common/Logger.h"
int main() {
Logger::Init(false, false);
Logger::Debug("Test");
string path = Functions::GetAppPath();
Logger::Debug("App Path: ", path.c_str());
return 0;
}
The functions file that is being referenced uses boost, and here is the makefile for this app that uses the library:
CC=g++
CFLAGS=-Wl,--verbose -std=c++11 -g -c -pedantic -Wall -Wextra -I../boost_1_57_0 -L../boost_1_57_0/stage/lib
LDFLAGS=-I../boost_1_57_0 -L../boost_1_57_0/stage/lib -L../boost_libs/lib -L/usr/lib64 -L/usr/kerberos/lib -L/usr/lib -L../Common
LIBS=-lz -lkrb5 -lk5crypto -lcom_err -lresolv -lm -lpthread -lrt -lboost_system -lboost_filesystem -lboost_thread -lboost_date_time -rdynamic -lcurl -lcommon
SOURCES = $(wildcard *.cpp)
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=testApp
GPROF=-pg
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $# $(LIBS) $(GPROF)
.cpp.o:
$(CC) $(CPPFLAGS) $(CFLAGS) $< -o $# $(GPROF)
clean:
rm -rf *o $(OBJECTS)
When I try to compile, I get the following errors, complaining about undefined references to boost:
g++ -Wl,--verbose -std=c++11 -g -c -pedantic -Wall -Wextra -I../boost_1_57_0 -L../boost_1_57_0/stage/lib main.cpp -o main.o -pg
g++ -I../boost_1_57_0 -L../boost_1_57_0/stage/lib -L../boost_libs/lib -L/usr/lib64 -L/usr/kerberos/lib -L/usr/lib -L../Common main.o -o testApp -lz -lkrb5 -lk5crypto -lcom_err -lresolv -lm -lpthread -lrt -lboost_system -lboost_filesystem -lboost_thread -lboost_date_time -rdynamic -lcurl -lcommon -pg
../Common/libcommon.a(Functions.o): In function `Functions::GetAppPath()':
/media/software/Robots/Common/Functions.cpp:43: undefined reference to `boost::filesystem::path::parent_path() const'
../Common/libcommon.a(Functions.o): In function `boost::filesystem::initial_path()':
/media/software/Robots/Common/../boost_1_57_0/boost/filesystem/operations.hpp:583: undefined reference to `boost::filesystem::detail::initial_path(boost::system::error_code*)'
../Common/libcommon.a(Functions.o): In function `boost::filesystem::system_complete(boost::filesystem::path const&)':
/media/software/Robots/Common/../boost_1_57_0/boost/filesystem/operations.hpp:655: undefined reference to `boost::filesystem::detail::system_complete(boost::filesystem::path const&, boost::system::error_code*)'
../Common/libcommon.a(Functions.o): In function `unsigned short boost::date_time::month_str_to_ushort<boost::gregorian::greg_month>(std::string const&)':
/media/software/Robots/Common/../boost_1_57_0/boost/date_time/date_parsing.hpp:67: undefined reference to `boost::gregorian::greg_month::get_month_map_ptr()'
../Common/libcommon.a(Logger.o): In function `Logger::Cleanup()':
/media/software/Robots/Common/Logger.cpp:84: undefined reference to `boost::thread::interrupt()'
../Common/libcommon.a(Logger.o): In function `Logger::InitFile(std::string, bool, std::_Ios_Openmode)':
/media/software/Robots/Common/Logger.cpp:186: undefined reference to `boost::filesystem::path::parent_path() const'
/media/software/Robots/Common/Logger.cpp:187: undefined reference to `boost::filesystem::path::parent_path() const'
../Common/libcommon.a(Logger.o): In function `Logger::Process()':
/media/software/Robots/Common/Logger.cpp:241: undefined reference to `boost::this_thread::interruption_point()'
../Common/libcommon.a(Logger.o): In function `boost::detail::thread_data_base::thread_data_base()':
/media/software/Robots/Common/../boost_1_57_0/boost/thread/pthread/thread_data.hpp:143: undefined reference to `vtable for boost::detail::thread_data_base'
../Common/libcommon.a(Logger.o): In function `boost::thread::start_thread()':
/media/software/Robots/Common/../boost_1_57_0/boost/thread/detail/thread.hpp:179: undefined reference to `boost::thread::start_thread_noexcept()'
../Common/libcommon.a(Logger.o): In function `boost::thread::~thread()':
/media/software/Robots/Common/../boost_1_57_0/boost/thread/detail/thread.hpp:254: undefined reference to `boost::thread::detach()'
../Common/libcommon.a(Logger.o): In function `boost::thread::get_id() const':
/media/software/Robots/Common/../boost_1_57_0/boost/thread/detail/thread.hpp:741: undefined reference to `boost::thread::native_handle()'
../Common/libcommon.a(Logger.o): In function `boost::thread::join()':
/media/software/Robots/Common/../boost_1_57_0/boost/thread/detail/thread.hpp:767: undefined reference to `boost::thread::join_noexcept()'
../Common/libcommon.a(Logger.o): In function `boost::filesystem::exists(boost::filesystem::path const&)':
/media/software/Robots/Common/../boost_1_57_0/boost/filesystem/operations.hpp:404: undefined reference to `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)'
../Common/libcommon.a(Logger.o): In function `boost::filesystem::create_directories(boost::filesystem::path const&)':
/media/software/Robots/Common/../boost_1_57_0/boost/filesystem/operations.hpp:523: undefined reference to `boost::filesystem::detail::create_directories(boost::filesystem::path const&, boost::system::error_code*)'
../Common/libcommon.a(Logger.o): In function `boost::detail::thread_data<void (*)()>::~thread_data()':
/media/software/Robots/Common/../boost_1_57_0/boost/thread/detail/thread.hpp:90: undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
../Common/libcommon.a(Logger.o):(.rodata._ZTIN5boost6detail11thread_dataIPFvvEEE[_ZTIN5boost6detail11thread_dataIPFvvEEE]+0x10): undefined reference to `typeinfo for boost::detail::thread_data_base'
collect2: error: ld returned 1 exit status
Needless to say that the project as a whole compiles with no issue.
This is my first attempt at building a c++ linux library and using it in another application, so I might be doing a rookie mistake, so bear that in mind.
Help is very much appreciated.
Thank you.
The order of libraries on the command line is significant to the linker. It processes libraries from left to right, resolving as-yet unresolved references already known to it against any functions provided by the current library.
Moreover, and particularly relevant here, if a given library contains references to functions that it does not itself provide then the linker does not resolve those against libraries it has already processed. That's why you have unresolved references: you have -lcommon last among your libraries, so function references therein will be resolved against only the C standard library.
It is valid to list libraries more than once in the link command, and sometimes that is needed, but in this case I think it makes sense simply to put -lcommon as the first library instead of the last. That will resolve the issue. You can even conceptualize this as putting the library most closely related to the main program closest to it in the link command.

Irregular Makefile Behaviour?

I'm compiling a program with 3 source files: main.cpp, DataHandler.cpp and DataHandler.h.
I use a makefile that contains the following code to build:
Makefile
OBJECTS = main.o DataHandler.o
CC = g++
DEBUG = -g
CFLAGS = -c -std=c++11
LFLAGS = -lopendnp3 -lopenpal -lasiodnp3 -lasiopal -lpthread -std=c++11
lvoutstation : $(OBJECTS)
$(CC) $(LFLAGS) $(OBJECTS) -o lvoutstation
main.o : main.cpp DataHandler.h
$(CC) $(CFLAGS) main.cpp
DataHandler.o : DataHandler.cpp DataHandler.h
$(CC) $(CFLAGS) DataHandler.cpp
.PHONY : clean
clean :
rm lvoutstation $(OBJECTS)
It build main.o and DataHandler.o fine, but when it gets to compiling the action executable, it gives linking erros to all my asiodnp3 and opendnp3 namespace function / class calls.
When I run the following command:
g++ -o lvoutstation main.o DataHandler.o -lopendnp3 -lopenpal -lasiodnp3
-lasiopal -lpthread -std=c++11
It works fine..
I don't understand where the linking error comes in?
EDIT
Upon request for the error message:
g++ -lopendnp3 -lopenpal -lasiodnp3 -lasiopal -std=c++11 main.o DataHandler.o -o lvoutstation
main.o: In function `main':
main.cpp:(.text+0x85): undefined reference to `asiodnp3::DNP3Manager::DNP3Manager(unsigned int, openpal::ICryptoProvider*, std::function<void ()>, std::function<void ()>)'
main.cpp:(.text+0xb7): undefined reference to `asiodnp3::DNP3Manager::AddLogSubscriber(openpal::ILogHandler*)'
main.cpp:(.text+0xf0): undefined reference to `opendnp3::ChannelRetry::Default()'
main.cpp:(.text+0x123): undefined reference to `asiodnp3::DNP3Manager::AddTCPServer(char const*, unsigned int, opendnp3::ChannelRetry const&, std::string const&, unsigned short)'
main.cpp:(.text+0x19f): undefined reference to `opendnp3::EventBufferConfig::AllTypes(unsigned short)'
main.cpp:(.text+0x1d6): undefined reference to `opendnp3::DefaultOutstationApplication::Instance()'
main.cpp:(.text+0x2c8): undefined reference to `asiodnp3::DNP3Manager::~DNP3Manager()'
main.o: In function `opendnp3::LinkConfig::LinkConfig(bool, bool)':
main.cpp:(.text._ZN8opendnp310LinkConfigC2Ebb[_ZN8opendnp310LinkConfigC5Ebb]+0x75): undefined reference to `openpal::TimeDuration::Seconds(long)'
main.cpp:(.text._ZN8opendnp310LinkConfigC2Ebb[_ZN8opendnp310LinkConfigC5Ebb]+0x87): undefined reference to `openpal::TimeDuration::Minutes(long)'
main.o: In function `opendnp3::OutstationConfig::OutstationConfig()':
main.cpp:(.text._ZN8opendnp316OutstationConfigC2Ev[_ZN8opendnp316OutstationConfigC5Ev]+0x14): undefined reference to `opendnp3::OutstationParams::OutstationParams()'
main.cpp:(.text._ZN8opendnp316OutstationConfigC2Ev[_ZN8opendnp316OutstationConfigC5Ev]+0x56): undefined reference to `opendnp3::EventBufferConfig::EventBufferConfig(unsigned short, unsigned short, unsigned short, unsigned short, unsigned short, unsigned short, unsigned short, unsigned short)'
main.o: In function `asiodnp3::ConsoleLogger::Instance()':
main.cpp:(.text._ZN8asiodnp313ConsoleLogger8InstanceEv[_ZN8asiodnp313ConsoleLogger8InstanceEv]+0x5): undefined reference to `asiodnp3::ConsoleLogger::instance'
DataHandler.o: In function `DataHandler::ReadMeasurements(asiodnp3::IOutstation*)':
DataHandler.cpp:(.text+0xfe): undefined reference to `asiodnp3::MeasUpdate::MeasUpdate(asiodnp3::IOutstation*)'
DataHandler.cpp:(.text+0x11d): undefined reference to `opendnp3::Analog::Analog(double)'
DataHandler.cpp:(.text+0x137): undefined reference to `asiodnp3::MeasUpdate::Update(opendnp3::Analog const&, unsigned short, opendnp3::EventMode)'
DataHandler.cpp:(.text+0x159): undefined reference to `asiodnp3::MeasUpdate::~MeasUpdate()'
DataHandler.cpp:(.text+0x17b): undefined reference to `asiodnp3::MeasUpdate::~MeasUpdate()'
collect2: error: ld returned 1 exit status
make: *** [lvoutstation] Error 1
The makefile just works as expected. Your action becomes actually different from your command line sample
$(CC) $(LFLAGS) $(OBJECTS) -o lvoutstation
will expand to
g++ -lopendnp3 -lopenpal -lasiodnp3 -lasiopal -lpthread -std=c++11 \
main.o DataHandler.o -o lvoutstation
The order of object files and libraries matters for the linking process.
You can fix the linker errors providing the libraries after the object files:
$(CC) $(OBJECTS) $(LFLAGS) -o lvoutstation

Build and Link using Makefiles for C/C++ in Linux

Below is my makefile
#Makefile for beaglebone
#General tools
CC=gcc
CFLAGS = -g -Wall
RM = rm -fr
TARGET = beaglebone
# Source locations
BACNET_CORE = ../../src
BACNET_INCLUDE = ../../include
BACNET_HANDLER = ../../demo/handler
BACNET_OBJECT = ../../demo/object
BACNET_DEMO = ../../demo
# local files for this project
CSRC = main.c arcnet.c bip-init.c dlmstp.c dlmstp_linux.c ethernet.c rs485.c timer.c device.c
#common demo files needed
DEMOSRC = $(BACNET_DEMO)/handler/txbuf.c $(BACNET_DEMO)/handler/h_npdu.c $(BACNET_DEMO)/handler/s_iam.c $(BACNET_DEMO)/handler/noserv.c
# core BACnet stack files
CORESRC = $(BACNET_CORE)/crc.c $(BACNET_CORE)/npdu.c $(BACNET_CORE)/bacdcode.c $(BACNET_CORE)/bacint.c $(BACNET_CORE)/bacreal.c \
$(BACNET_CORE)/bacstr.c $(BACNET_CORE)/iam.c $(BACNET_CORE)/rp.c $(BACNET_CORE)/wp.c $(BACNET_CORE)/whois.c $(BACNET_CORE)/bacaddr.c \
$(BACNET_CORE)/abort.c $(BACNET_CORE)/reject.c $(BACNET_CORE)/bacerror.c $(BACNET_CORE)/bacapp.c
## Include Directories
INCLUDES = -I. -I$(BACNET_INCLUDE)
INCLUDES += -I$(BACNET_OBJECT)
INCLUDES += -I$(BACNET_HANDLER)
# Source to Object conversion
COBJ = $(CSRC:.c=.o)
DEMOOBJ = $(DEMOSRC:.c=.o)
COREOBJ = $(CORESRC:.c=.o)
OBJECTS = $(COBJ) $(DEMOOBJ) $(COREOBJ)
#Build and Link the objects
all: $(TARGET)
.c.o:
$(CC) -c $(INCLUDES) $(CFLAGS) $*.c -o $#
$(TARGET) : $(OBJECTS)
$(CC) $(CFLAGS) $(INCLUDES) $(OBJECTS) -o $(TARGET)
.PHONY: clean
clean:
$(RM) *.o *~ $(TARGET)
when issue make I get the .o files created and it does not creates an executable. I get the output as follows:
s_iam.c:(.text+0xed): undefined reference to `bip_get_my_address'
../../demo/handler/s_iam.o: In function `Send_I_Am_Unicast':
s_iam.c:(.text+0x16c): undefined reference to `bvlc_send_pdu'
../../demo/handler/noserv.o: In function `handler_unrecognized_service':
noserv.c:(.text+0x14): undefined reference to `bip_get_my_address'
noserv.c:(.text+0x6b): undefined reference to `bvlc_send_pdu'
../../src/bacapp.o: In function `bacapp_copy':
bacapp.c:(.text+0x7ac): undefined reference to `datetime_copy_date'
bacapp.c:(.text+0x7bb): undefined reference to `datetime_copy_time'
../../src/bacapp.o: In function `bacapp_snprintf_value':
bacapp.c:(.text+0xba4): undefined reference to `bactext_object_type_name'
bacapp.c:(.text+0xbbc): undefined reference to `bactext_event_state_name'
bacapp.c:(.text+0xbce): undefined reference to `bactext_engineering_unit_name'
bacapp.c:(.text+0xbe1): undefined reference to `bactext_binary_polarity_name'
bacapp.c:(.text+0xbf0): undefined reference to `bactext_binary_present_value_name'
bacapp.c:(.text+0xbfa): undefined reference to `bactext_reliability_name'
bacapp.c:(.text+0xc04): undefined reference to `bactext_device_status_name'
bacapp.c:(.text+0xc0e): undefined reference to `bactext_segmentation_name'
bacapp.c:(.text+0xc18): undefined reference to `bactext_node_type_name'
bacapp.c:(.text+0xc45): undefined reference to `bactext_day_of_week_name'
bacapp.c:(.text+0xc84): undefined reference to `bactext_month_name'
bacapp.c:(.text+0xe28): undefined reference to `bactext_object_type_name'
../../src/bacapp.o: In function `bacapp_parse_application_data':
bacapp.c:(.text+0x10bd): undefined reference to `datetime_set_date'
collect2: error: ld returned 1 exit status
make: *** [beaglebone] Error 1
Kindly tell me where I am going wrong and many thanks for your help and suggestions!!!
Regards,
Gibson
Looks like your object files are in a bit of a random order, so that later object files refer to symbol defined in earlier object files and this is why the link fails.
Try the following. Replace:
$(TARGET) : $(OBJECTS)
$(CC) $(CFLAGS) $(INCLUDES) $(OBJECTS) -o $(TARGET)
With:
$(TARGET) : $(OBJECTS)
$(CC) -o $# -Wl,--start-group $(OBJECTS) -Wl,--end-group
That is going to make ld to scan the object files for missing symbols a few times (like MSVC does).

undefined reference with boost when I try to compile

i have lot of errors when i try to compile my server with boost.
Here, this is my makefile :
NAME = serveur
SRCS = Serveur/main.cpp \
Serveur/Client.cpp \
Serveur/Commande.cpp \
Serveur/My_exception.cpp \
Serveur/Network.cpp \
Serveur/Server.cpp
#####################################################
OBJS = $(SRCS:.cpp=.o)
CC = g++
RM = rm -f
CFLAGS = -g -W -Wall -Werror
INCL = ./Serveur/boost_1_47_0
LIB = ./Serveur/boost_1_47_0/stage/lib/
NLIB = -lboost_system -lboost_system-mt -lboost_filesystem -lboost_filesystem-mt
#####################################################
$(NAME) : $(OBJS)
#$(CC) $(OBJS) -I$(INCL) -L$(LIB) $(NLIB) -o $(NAME)
#printf "\n \033[33m[Message]\033[39m Compilation under Linux done\n\n"
.cpp.o :
#$(CC) $(CFLAGS) -I$(INCL) -L$(LIB) $(NLIB) -c $< -o $#
#printf " \033[34m[Compilation]\033[39m %s\n" $<
re : fclean all
all : $(NAME)
clean :
#$(RM) *~ $(OBJS)
#printf " \033[31m[Delete] \033[39m%s\n" $(OBJS)
fclean : clean
#$(RM) $(NAME)
#printf "\n \033[31m[Delete] \033[39m%s\n\n" $(NAME)
After "googling" i found that it's necessary to put -lboost_filesystem but it's not working again and there are next errors :
Serveur/main.o: In function `__static_initialization_and_destruction_0':
/home/abad_a//rendulocal/tech3/cpp/babel-2014-abad_a/Serveur/./Serveur/boost_1_47_0/boost/system/error_code.hpp:214: undefined reference to `boost::system::generic_category()'
/home/abad_a//rendulocal/tech3/cpp/babel-2014-abad_a/Serveur/./Serveur/boost_1_47_0/boost/system/error_code.hpp:215: undefined reference to `boost::system::generic_category()'
/home/abad_a//rendulocal/tech3/cpp/babel-2014-abad_a/Serveur/./Serveur/boost_1_47_0/boost/system/error_code.hpp:216: undefined reference to `boost::system::system_category()'
Serveur/main.o: In function `error_code':
/home/abad_a//rendulocal/tech3/cpp/babel-2014-abad_a/Serveur/./Serveur/boost_1_47_0/boost/system/error_code.hpp:315: undefined reference to `boost::system::system_category()'
Serveur/main.o: In function `boost::asio::error::get_system_category()':
/home/abad_a//rendulocal/tech3/cpp/babel-2014-abad_a/Serveur/./Serveur/boost_1_47_0/boost/asio/error.hpp:216: undefined reference to `boost::system::system_category()'
Serveur/Client.o: In function `__static_initialization_and_destruction_0':
/home/abad_a//rendulocal/tech3/cpp/babel-2014-abad_a/Serveur/./Serveur/boost_1_47_0/boost/system/error_code.hpp:214: undefined reference to `boost::system::generic_category()'
/home/abad_a//rendulocal/tech3/cpp/babel-2014-abad_a/Serveur/./Serveur/boost_1_47_0/boost/system/error_code.hpp:215: undefined reference to `boost::system::generic_category()'
/home/abad_a//rendulocal/tech3/cpp/babel-2014-abad_a/Serveur/./Serveur/boost_1_47_0/boost/system/error_code.hpp:216: undefined reference to `boost::system::system_category()'
Serveur/Network.o: In function `__static_initialization_and_destruction_0':
/home/abad_a//rendulocal/tech3/cpp/babel-2014-abad_a/Serveur/./Serveur/boost_1_47_0/boost/system/error_code.hpp:214: undefined reference to `boost::system::generic_category()'
/home/abad_a//rendulocal/tech3/cpp/babel-2014-abad_a/Serveur/./Serveur/boost_1_47_0/boost/system/error_code.hpp:215: undefined reference to `boost::system::generic_category()'
/home/abad_a//rendulocal/tech3/cpp/babel-2014-abad_a/Serveur/./Serveur/boost_1_47_0/boost/system/error_code.hpp:216: undefined reference to `boost::system::system_category()'
Serveur/Server.o: In function `__static_initialization_and_destruction_0':
/home/abad_a//rendulocal/tech3/cpp/babel-2014-abad_a/Serveur/./Serveur/boost_1_47_0/boost/system/error_code.hpp:214: undefined reference to `boost::system::generic_category()'
/home/abad_a//rendulocal/tech3/cpp/babel-2014-abad_a/Serveur/./Serveur/boost_1_47_0/boost/system/error_code.hpp:215: undefined reference to `boost::system::generic_category()'
/home/abad_a//rendulocal/tech3/cpp/babel-2014-abad_a/Serveur/./Serveur/boost_1_47_0/boost/system/error_code.hpp:216: undefined reference to `boost::system::system_category()'
collect2: ld returned 1 exit status
make: *** [serveur] Error 1
Anyone know why it's not working?
boost::filesystem depends on boost::system, so you'll have to add -lboost_system, also.
I am using cygwin and the solution to this problem was linking the libraries
libboost_filesystem-mt
libboost_system-mt
The invocation of the linker looked like this:
g++ -o "bin.exe" ./src/reader.o ./src/bin.o -llibboost_filesystem-mt -llibboost_system-mt