g++ the order of static library matters? - c++

I have been struggling all day for this problem. I use some libraries in my project and I always got some compilation errors complaining that undefined reference.
The relevant files are as follows:
encode.cc//the main file
url_codec.h//the header of the libraries, got some function definition in it
libimageenc.a
libmbpicenc.a
liburlaes.a
liburldecode.a
The makefile is like:
cflags = -Wall -O2 -fPIC
libpath=./libs/
libs+=$(libpath)liburlaes.a
libs+=$(libpath)liburldecode.a $(libpath)libimageenc.a $(libpath)libmbpicenc.a
cxx = g++
bin = encode
all: $(bin)
srcs = $(shell ls *.cc *.cpp)
objs = $(srcs:%.cc=%.o)
$(bin):${objs}
$(cxx) $(cflags) $(inc) -o $# ${objs} ${libs}
$(objs):%.o:%.cc
$(cxx) $(cflags) $(inc) -c -o $# $<
clean:
rm -f *.o
rm -f *.bak
rm -f $(bin)
The g++ compiler complains that:
g++ -Wall -O2 -fPIC -o encode encode.o ./libs/liburlaes.a ./libs/liburldecode.a ./libs/libimageenc.a ./libs/libmbpicenc.a
./libs/liburldecode.a(url_codec.o): In function `url_codec::offpic_url_decode_func(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, int&)':
url_codec.cc:(.text+0x18da): undefined reference to `uuid_unparse'
./libs/liburldecode.a(crypto_aes.o): In function `encode_aes':
crypto_aes.cc:(.text+0x4e): undefined reference to `AES_cbc_encrypt'
./libs/liburldecode.a(crypto_aes.o): In function `decode_aes':
crypto_aes.cc:(.text+0xae): undefined reference to `AES_cbc_encrypt'
./libs/liburldecode.a(crypto_aes.o): In function `init_aes_encrypt_key':
crypto_aes.cc:(.text+0xf3): undefined reference to `AES_set_encrypt_key'
./libs/liburldecode.a(crypto_aes.o): In function `init_aes_decrypt_key':
crypto_aes.cc:(.text+0x143): undefined reference to `AES_set_decrypt_key'
collect2: ld returned 1 exit status
But all these function are well defined in liburlaes.a as shown by nm -C
nm -C liburlaes.a | grep -i 'aes'
decode_byaes.o:
U AES_cbc_encrypt
U AES_set_decrypt_key
U AES_set_encrypt_key
00000060 T decode_byaes
00000000 T encode_byaes
00000110 T init_byaes_decrypt_key
000000c0 T init_byaes_encrypt_key
U decode_byaes
U encode_byaes
U init_byaes_decrypt_key
U init_byaes_encrypt_key
Things won't get better by moving the liburlaes.a to the end of libs, the output is exactly as above. And move libimageenc backward will make it worse, with more symbols claimed undefined:
libs+=$(libpath)liburldecode.a $(libpath)libimageenc.a $(libpath)libmbpicenc.a
libs+=$(libpath)liburlaes.a
So, how can I fix this?
UPDATES
I tried to put the liburlaes.a in both side, but it doesn't work, I wrap the libaray with '**' to emphasize :
g++ -Wall -O2 -fPIC -o encode encode.o **./libs/liburlaes.a** ./libs/liburldecode.a ./libs/libimageenc.a ./libs/libmbpicenc.a **./libs/liburlaes.a**
./libs/liburldecode.a(url_codec.o): In function `url_codec::offpic_url_decode_func(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, int&)':
url_codec.cc:(.text+0x18da): undefined reference to `uuid_unparse'
./libs/liburldecode.a(crypto_aes.o): In function `encode_aes':
crypto_aes.cc:(.text+0x4e): undefined reference to `AES_cbc_encrypt'
./libs/liburldecode.a(crypto_aes.o): In function `decode_aes':
crypto_aes.cc:(.text+0xae): undefined reference to `AES_cbc_encrypt'
./libs/liburldecode.a(crypto_aes.o): In function `init_aes_encrypt_key':
crypto_aes.cc:(.text+0xf3): undefined reference to `AES_set_encrypt_key'
./libs/liburldecode.a(crypto_aes.o): In function `init_aes_decrypt_key':
crypto_aes.cc:(.text+0x143): undefined reference to `AES_set_decrypt_key'
collect2: ld returned 1 exit status
make: *** [encode] Error 1

Yes, the order of libraries definitely matter. You need to put whatever uses the library first, then the library.
I have had cases where library A depends on something in library B, and library B needs something in library A, so you then need to put library A twice in the list.
The way the linker works is that it processes the object files, then reads the libraries to resolve the symbols not present in the object files. If the library has the object "files" to solve the dependency, then those parts are included. It then goes on to the next library. It doesn't "remember" what it has seen in the previous libraries.

How about this,just put liburlaes.a right behind the liburldecode.a.
g++ -Wall -O2 -fPIC -o encode encode.o ./libs/liburldecode.a **./libs/liburlaes.a **./libs/libimageenc.a ./libs/libmbpicenc.a

Related

How to fix makefile to properly include -lcrypto to avoid linker error: undefined reference to `EVP_CIPHER_CTX_new'

First of all I am trying to use
#include <openssl/evp.h>
I am getting the common linker error:
undefined reference to `EVP_CIPHER_CTX_new'
which I know can be solved by using -lcrypto when compiling. I am able to do this when compiling manually i.e
g++ encrv2.cpp -o encrv2 -lcrypto
though I am trying to create a Makefile and putting my encryption/decryption functions in a seperate header file (AesGcm) and am unable to get around the linker issue. Below is my Makefile so far and the output when I try and make all.
CXX = g++
CXXFLAGS = -std=c++14
LDFLAGS= -lcrypto
all: encrv2
AesGcm.o: AesGcm.cpp AesGcm.h
$(CXX) $(CXXFLAGS) -c AesGcm.cpp $(LDFLAGS)
encrv2.o: encrv2.cpp AesGcm.h
$(CXX) $(CXXFLAGS) -c encrv2.cpp $(LDFLAGS)
encrv2: encrv2.o AesGcm.o
clean:
rm -f *~ *.o encrv2
Giving the following error:
cc -lcrypto encrv2.o AesGcm.o -o encrv2
AesGcm.o: In function `AesGcm::encrypt(unsigned char*, int, unsigned char*, int, unsigned char*, unsigned char*, unsigned char*, unsigned char*)':
AesGcm.cpp:(.text+0x3e): undefined reference to `EVP_CIPHER_CTX_new'
AesGcm.cpp:(.text+0x58): undefined reference to `EVP_aes_256_gcm'
AesGcm.cpp:(.text+0x77): undefined reference to `EVP_EncryptInit_ex'
AesGcm.cpp:(.text+0xae): undefined reference to `EVP_EncryptInit_ex'
AesGcm.cpp:(.text+0xe0): undefined reference to `EVP_EncryptUpdate'
AesGcm.cpp:(.text+0x111): undefined reference to `EVP_EncryptUpdate'
AesGcm.cpp:(.text+0x148): undefined reference to `EVP_EncryptFinal_ex'
AesGcm.cpp:(.text+0x17b): undefined reference to `EVP_CIPHER_CTX_ctrl'
AesGcm.cpp:(.text+0x197): undefined reference to `EVP_CIPHER_CTX_free'
AesGcm.o: In function `AesGcm::decrypt(unsigned char*, int, unsigned char*, int, unsigned char*, unsigned char*, unsigned char*, unsigned char*)':
AesGcm.cpp:(.text+0x30a): undefined reference to `EVP_CIPHER_CTX_new'
AesGcm.cpp:(.text+0x324): undefined reference to `EVP_aes_256_gcm'
AesGcm.cpp:(.text+0x343): undefined reference to `EVP_DecryptInit_ex'
AesGcm.cpp:(.text+0x37a): undefined reference to `EVP_DecryptInit_ex'
AesGcm.cpp:(.text+0x3ac): undefined reference to `EVP_DecryptUpdate'
AesGcm.cpp:(.text+0x3dd): undefined reference to `EVP_DecryptUpdate'
AesGcm.cpp:(.text+0x410): undefined reference to `EVP_CIPHER_CTX_ctrl'
AesGcm.cpp:(.text+0x441): undefined reference to `EVP_DecryptFinal_ex'
AesGcm.cpp:(.text+0x450): undefined reference to `EVP_CIPHER_CTX_free'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'encrv2' failed
make: *** [encrv2] Error 1
You're using the wrong variable.
LDFLAGS are intended to be used for linker flags such as -L, etc. that need to come "earlier" in the link line. That variable is not intended to have libraries that are added to the link line.
You should be using the LDLIBS variable for that:
LDLIBS = -lcrypto
The default rule for linking, which is what you're using since you don't write one yourself, looks something like this:
%: %.o
# recipe to execute (built-in):
$(CC) $(LDFLAGS) $(TARGET_ARCH) $^ $(LDLIBS) -o $#
Note that LDLIBS comes after your object files, which is what you need to avoid this linker error. Order of object files versus libraries, and between libraries themselves, is very important to the linker.

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.

file was built for archive which is not the architecture being linked (x86_64)

I use makefile to compile my C++ program, but it shows a warning:
make
g++ -g -std=c++0x -o ns-client main.cpp Client.cpp TCPConnect.cpp RSAsample.cpp libStatic/libchilkat_i386.a libStatic/libchilkat_x86_64.a -lpthread
ld: warning: ignoring file libStatic/libchilkat_i386.a, file was built for archive which is not the architecture being linked (x86_64): libStatic/libchilkat_i386.a
My makefile is as follows:
cat Makefile
LIBS = libStatic/libchilkat_i386.a libStatic/libchilkat_x86_64.a -lpthread
GPP = g++ -g -std=c++0x
TARGET = -o ns-client
CPP = main.cpp Client.cpp TCPConnect.cpp RSAsample.cpp
all:
$(GPP) $(TARGET) $(CPP) $(LIBS)
clean:
rm ns-client
libchilkat_i386.a and libchilkat_x86_64.a are downloaded by me, not implemented by me. I put them under a directory called "libStatic".
These are all my C++ files and lib:
ls
Client.cpp RSAsample.cpp libStatic
Client.h RSAsample.h main.cpp
Client_Common.h TCPConnect.cpp ns-client
Common_Num_Define.h TCPConnect.h ns-client.dSYM
Common_Protocol.h include ns-server
Makefile libDyn
In libStatic directory, it has:
ls
libchilkat_i386.a libchilkat_x86_64.a
I don't know why this warning happens. Even though it has this warning, it still compile successfully. But when I upload them to our school's server and compile them(I definitely upload these static libs), it shows:
undefined reference to
Many these kinds of errors. For example:
/home/users/liuly/liuly/networksecurity/project/cplus/client/Client.cpp:17: undefined reference to `CkRsa::exportPublicKey()'
All these functions and vars which are mentioned in these kinds of errors should be included in libchilkat_i386.a libchilkat_x86_64.a
So I doubt maybe it is because of my Makefile.But I am not sure. I also doubt that maybe related to the first warning. Can somebody tells me how solve the first warning or why it cannot compile on another machine but can compile on my own machine? Thanks for everyone's help!
This is all errors it showed on our school's server:
/tmp/ccA32Kao.o: In function `My_RSA::My_RSA()':
main.cpp:(.text._ZN6My_RSAC2Ev[_ZN6My_RSAC5Ev]+0x19): undefined reference to `CkRsa::CkRsa()'
main.cpp:(.text._ZN6My_RSAC2Ev[_ZN6My_RSAC5Ev]+0x2b): undefined reference to `CkRsa::CkRsa()'
main.cpp:(.text._ZN6My_RSAC2Ev[_ZN6My_RSAC5Ev]+0x3d): undefined reference to `CkRsa::CkRsa()'
main.cpp:(.text._ZN6My_RSAC2Ev[_ZN6My_RSAC5Ev]+0x54): undefined reference to `CkRsa::~CkRsa()'
main.cpp:(.text._ZN6My_RSAC2Ev[_ZN6My_RSAC5Ev]+0x69): undefined reference to `CkRsa::~CkRsa()'
/tmp/ccA32Kao.o: In function `My_RSA::~My_RSA()':
main.cpp:(.text._ZN6My_RSAD2Ev[_ZN6My_RSAD5Ev]+0x1a): undefined reference to `CkRsa::~CkRsa()'
main.cpp:(.text._ZN6My_RSAD2Ev[_ZN6My_RSAD5Ev]+0x2c): undefined reference to `CkRsa::~CkRsa()'
main.cpp:(.text._ZN6My_RSAD2Ev[_ZN6My_RSAD5Ev]+0x3c): undefined reference to `CkRsa::~CkRsa()'
/tmp/cciEnFVR.o: In function `Client::Register()':
/home/users/liuly/liuly/networksecurity/project/cplus/client/Client.cpp:17: undefined reference to `CkRsa::exportPublicKey()'
/home/users/liuly/liuly/networksecurity/project/cplus/client/Client.cpp:18: undefined reference to `CkRsa::exportPrivateKey()'
/tmp/cciEnFVR.o: In function Client::Login()':
/home/users/liuly/liuly/networksecurity/project/cplus/client/Client.cpp:128: undefined reference toCkRsa::exportPublicKey()'
/home/users/liuly/liuly/networksecurity/project/cplus/client/Client.cpp:129: undefined reference to CkRsa::exportPrivateKey()'
/tmp/cciEnFVR.o: In functionClient::Judge_Command(char*)':
/home/users/liuly/liuly/networksecurity/project/cplus/client/Client.cpp:930 : undefined reference to CkRsa::exportPrivateKey()'
/home/users/liuly/liuly/networksecurity/project/cplus/client/Client.cpp:931: undefined reference toCkRsa::exportPublicKey()'
/tmp/ccRMXreb.o: In function My_RSA::MyEncryption(char const*, char const*)':
/home/users/liuly/liuly/networksecurity/project/cplus/client/RSAsample.cpp: 16: undefined reference toCkRsa::put_EncodingMode(char const*)'
/home/users/liuly/liuly/networksecurity/project/cplus/client/RSAsample.cpp: 17: undefined reference to CkRsa::ImportPublicKey(char const*)'
/home/users/liuly/liuly/networksecurity/project/cplus/client/RSAsample.cpp: 25: undefined reference toCkRsa::encryptStringENC(char const*, bool)'
/tmp/ccRMXreb.o: In function My_RSA::MyDecryption(char*)':
/home/users/liuly/liuly/networksecurity/project/cplus/client/RSAsample.cpp: 33: undefined reference toCkRsa::put_EncodingMode(char const*)'
/home/users/liuly/liuly/networksecurity/project/cplus/client/RSAsample.cpp: 34: undefined reference to CkRsa::ImportPrivateKey(char const*)'
/home/users/liuly/liuly/networksecurity/project/cplus/client/RSAsample.cpp: 42: undefined reference toCkRsa::decryptStringENC(char const*, bool)'
/tmp/ccRMXreb.o: In function My_RSA::My_Initial_Key()':
/home/users/liuly/liuly/networksecurity/project/cplus/client/RSAsample.cpp: 52: undefined reference toCkRsa::UnlockComponent(char const*)'
/home/users/liuly/liuly/networksecurity/project/cplus/client/RSAsample.cpp: 58: undefined reference to CkRsa::GenerateKey(int)'
/home/users/liuly/liuly/networksecurity/project/cplus/client/RSAsample.cpp: 61: undefined reference toCkMultiByteBase::lastErrorText()'
collect2: error: ld returned 1 exit status
make: *** [all] Error 1
All these functions which should belong to CKRsa are in #include "include/CkRsa.h", but I definitely create the "include" directory on our school's server and upload these CKRsa.h files in that directory.
I revise my makefile,but it still doesn't work:
cat Makefile
LFLAGS += -L /home/users/liuly/liuly/networksecurity/project/cplus/client/libStatic
LIBS = libStatic/libchilkat_x86_64.a -lpthread
GPP = g++ -g -std=c++0x
TARGET = -o ns-client
CPP = main.cpp Client.cpp TCPConnect.cpp Common_Protocol.h RSAsample.cpp TCPConnect.h Client.h RSAsample.h include/CkRsa.h
all:
$(GPP) $(LFLAGS) $(TARGET) $(CPP) $(LIBS)
I revise makefile again, but it still doesn't work:
cat Makefile
LFLAGS += -L /home/users/liuly/liuly/networksecurity/project/cplus/client/libStatic
LIBS = -lchilkat_x86_64 -lpthread
GPP = g++ -g -std=c++0x
TARGET = -o ns-client
CPP = main.cpp Client.cpp TCPConnect.cpp Common_Protocol.h RSAsample.cpp TCPConnect.h Client.h RSAsample.h include/CkRsa.h
all:
$(GPP) $(LFLAGS) $(TARGET) $(CPP) $(LIBS)
The library problem (built for a different architecture) is easily fixed by NOT using the library that you don't want. If you are publishing a makefile, you'll have to use some sort of configuration setting to determine the right architecture. If it's just your own project, pick the one that matches your system (by the looks of your makefile, the x86_variant).
The error CkRsa::exportPublicKey is caused by your client code: The official symbol is CkRsa::ExportPublicKey - note the uppercase E at the beginning of the function name.
Here's the header file:
http://smart-refrigerator-hbnu.googlecode.com/svn/branches/kmy/RFID_CLIENT_/include/CkRsa.h
Now I see why this happens. My professor tells me that the static library which is downloaded from internet is only used for MAC OS (my own laptop is MAC), but our school's server is ubuntu. So i need to download the ubuntu version,otherwise our server cannot recognize the static lib. nm -a libchilkat_x86_64.a |less using this command to check whether it can be recognized.
When I use this command on our server,it shows:
nm: C_CkString.o: File format not recognized etc

Build brief module

I trying to build brief software. Firstly I create the ./lib/libbrief.so, and secondly I am trying to build main file. The makefile which included in .zip file:
CC=g++
SOURCES=main.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=main
//#Only enable -msse4.2 on CPUs supporting the POPCNT instruction
CFLAGS = -Wall -DNDEBUG -O3 -march=nocona #-msse4.2
//#CFLAGS = -Wall -DDEBUG -g -O0 -fno-inline-functions
LDFLAGS = -Wl
//# BRIEF
CFLAGS += -I../brief/include
LDFLAGS += -L../brief/lib -lbrief
//# OpenCV
CFLAGS += `pkg-config opencv --cflags`
LDFLAGS += `pkg-config opencv --libs`
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $#
.cpp.o:
$(CC) -g -c $(CFLAGS) $< -o $#
clean:
rm -f $(OBJECTS) $(EXECUTABLE) matches.png
However I am getting errors, related with opencv. The errors found:
g++ -Wl -L../brief/lib -lbrief `pkg-config opencv --libs` main.o -o main
main.o: In function `~BRIEF':
Desktop/asdf/brief_v1.0/test_app/../brief/include/brief/BRIEF.hpp:203:
undefined reference to `cvReleaseImage'
Desktop/asdf/brief_v1.0/test_app/../brief/include/brief/BRIEF.hpp:204:
undefined reference to `cvReleaseImage'
main.o: In function `BRIEF':
Desktop/asdf/brief_v1.0/test_app/../brief/include/brief/BRIEF.hpp:156:
undefined reference to `cvCreateImage'
Desktop/asdf/brief_v1.0/test_app/../brief/include/brief/BRIEF.hpp:157:
undefined reference to `cvCreateImage'
main.o: In function `TestSampler<signed char>::sampleGaussian(signed char*, int, int)'
main.o: In function `BRIEF':
Desktop/asdf/brief_v1.0/test_app/../brief/include/brief/BRIEF.hpp:156:
undefined reference to `cvCreateImage'
Desktop/asdf/brief_v1.0/test_app/../brief/include/brief/BRIEF.hpp:157:
undefined reference to `cvCreateImage'
main.o: In function `TestSampler<signed char>::sampleGaussian(signed char*, int, int)':
main.o: In function `BRIEF::writeTests(std::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&) const':
Desktop/asdf/brief_v1.0/test_app/../brief/include/brief/BRIEF.hpp:511:
undefined reference to `utils::stdoutError(std::basic_string<char,
std::char_traits<char>,
std::allocator<char> >, char const*, int, char const*)'
main.o: In function `BRIEF::readTests(std::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&)':
Desktop/asdf/brief_v1.0/test_app/../brief/include/brief/BRIEF.hpp:524:
undefined reference to `utils::stdoutError(std::basic_string<char,
std::char_traits<char>,
std::allocator<char> >, char const*, int, char const*)'
main.o: In function `detectSURF':
Desktop/asdf/brief_v1.0/test_app/main.cpp:92: undefined reference to
`cvCreateMemStorage'
Desktop/asdf/brief_v1.0/test_app/main.cpp:101: undefined reference to
`cvExtractSURF'
Desktop/asdf/brief_v1.0/test_app/main.cpp:106: undefined reference to
`cvGetSeqElem'
main.o: In function `timeDescription(int)':
Desktop/asdf/brief_v1.0/test_app/main.cpp:201: undefined reference to
`cvLoadImage'
Desktop/asdf/brief_v1.0/test_app/main.cpp:214: undefined reference to
`cvReleaseImage'
main.o: In function `~BRIEF':
What have to do to build proper the main.cpp?
You must list all the object files on the link like before the libraries that they use.
See, for example, Why does the order in which libraries are linked sometimes cause errors in GCC?

Boost::system link error on Ubuntu

I'm trying to compile the following c++ code that implements Context Tree Switching (More info on the download page):
Zip archive, 0.2 MB
which requires some boost libraries. I download the latest version from boost.org and built all libraries that needed building following the instructions on the website. I also modified the makefile included in the archive to add the boost lib path and boost_system, but I still get an error. Here's the makefile i'm using:
PROGRAM = cts
SOURCES = $(wildcard *.cpp)
OBJECTS = $(SOURCES:.cpp=.o)
CFLAGS = -Wall
LDFLAGS = -lboost_program_options -lboost_filesystem -lboost_system
$(PROGRAM): $(OBJECTS) Makefile
g++ $(CFLAGS) -L/home/users/mnembrini/opt/boost/lib $(LDFLAGS) -o $(PROGRAM) $(OBJECTS)
# Include known dependecies from -MMD
#-include $(OBJECTS:.o=.d)
%.o: %.cpp
g++ -MMD $(CFLAGS) -I/home/users/mnembrini/opt/boost/include -c $<
clean:
rm -f $(OBJECTS) *.d
.PHONY: clean
where boost is in ~/opt/boost (constains lib and include subdir). And here's the linking error:
mnembrini#meem:~/src/cts-v1 $ make
g++ -MMD -Wall -I/home/users/mnembrini/opt/boost/include -c ac.cpp
g++ -MMD -Wall -I/home/users/mnembrini/opt/boost/include -c cts.cpp
cts.cpp: In member function ‘virtual void SwitchingTree::update(bit_t)’:
cts.cpp:402:12: warning: variable ‘snc’ set but not used [-Wunused-but-set-variable]
cts.cpp: In member function ‘virtual double SwitchingTree::prob(bit_t)’:
cts.cpp:432:12: warning: variable ‘snc’ set but not used [-Wunused-but-set-variable]
g++ -MMD -Wall -I/home/users/mnembrini/opt/boost/include -c ctw.cpp
g++ -MMD -Wall -I/home/users/mnembrini/opt/boost/include -c icsilog.cpp
g++ -MMD -Wall -I/home/users/mnembrini/opt/boost/include -c main.cpp
g++ -MMD -Wall -I/home/users/mnembrini/opt/boost/include -c PowFast.cpp
g++ -Wall -L/home/users/mnembrini/opt/boost/lib -lboost_program_options -lboost_filesystem -lboost_system -o cts ac.o cts.o ctw.o icsilog.o main.o PowFast.o
cts.o: In function `__static_initialization_and_destruction_0(int, int)':
cts.cpp:(.text+0x1743): undefined reference to `boost::system::generic_category()'
cts.cpp:(.text+0x174f): undefined reference to `boost::system::generic_category()'
cts.cpp:(.text+0x175b): undefined reference to `boost::system::system_category()'
ctw.o: In function `__static_initialization_and_destruction_0(int, int)':
ctw.cpp:(.text+0xfcf): undefined reference to `boost::system::generic_category()'
ctw.cpp:(.text+0xfdb): undefined reference to `boost::system::generic_category()'
ctw.cpp:(.text+0xfe7): undefined reference to `boost::system::system_category()'
main.o: In function `showHelp()':
main.cpp:(.text+0x1c): undefined reference to `boost::program_options::operator<<(std::basic_ostream<char, std::char_traits<char> >&, boost::program_options::options_description const&)'
main.o: In function `initOptions(int, char**, boost::program_options::variables_map&)':
main.cpp:(.text+0x10f): undefined reference to `boost::program_options::options_description::add_options()'
main.cpp:(.text+0x129): undefined reference to `boost::program_options::options_description_easy_init::operator()(char const*, char const*)'
main.cpp:(.text+0x13e): undefined reference to `boost::program_options::options_description_easy_init::operator()(char const*, boost::program_options::value_semantic const*, char const*)'
main.cpp:(.text+0x153): undefined reference to `boost::program_options::options_description_easy_init::operator()(char const*, boost::program_options::value_semantic const*, char const*)'
main.cpp:(.text+0x166): undefined reference to `boost::program_options::options_description_easy_init::operator()(char const*, boost::program_options::value_semantic const*, char const*)'
main.cpp:(.text+0x1d6): undefined reference to `boost::program_options::store(boost::program_options::basic_parsed_options<char> const&, boost::program_options::variables_map&, bool)'
main.cpp:(.text+0x200): undefined reference to `boost::program_options::notify(boost::program_options::variables_map&)'
main.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x1f13): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0x1f1f): undefined reference to `boost::system::generic_category()'
[snip (2-3 screens like above)]
collect2: ld returned 1 exit status
make: *** [cts] Error 1
I'm using Gcc 4.6.3 on Ubuntu 12.04 64bit.
Place all the libraries after all the object files within the command line. The order is important here, unlike on some other operating systems.
You need to (just like n.m earlier wrote) make sure you have the correct order of the linking.
Basically some implementations care about what order you link object but also libraries.
If you link a library that hasnt yet been referenced by previous code it will be discarded. I remember coming up with a solution to hack in different (versions of) libraries in a program by referencing the same symbol but in an object linked after the first one and then re-link another library version:
-lyourprojwantingv1 -llibraryofv1 -lyourprojwantingv2 -llibraryofv2
Personally i think this is all just madness! (All of it!)