How to link C++ Application with cppkafka - c++

As mentioned in the cppkafka official GitHub in usage section.
If you want to use cppkafka, you'll need to link your application with:
cppkafka
rdkafka
I tried to compile example program mentioned in the README.md of the GitHub with
gcc -O3 -Wall test.cpp -std=c++11 -lrdkafka -lpthread -lz -lstdc++ -D_GLIBCXX_USE_CXX11_ABI=0 -lcppkafka
It is throwing with following error
test.cpp:(.text.startup+0x46): undefined reference to `cppkafka::ConfigurationOption::ConfigurationOption(std::string const&, char const*)'
test.cpp:(.text.startup+0xd9): undefined reference to `cppkafka::Buffer::Buffer(std::string const&)'
collect2: error: ld returned 1 exit status

No need of -D_GLIBCXX_USE_CXX11_ABI=0 option while compiling.
you can simply compile this as following
gcc -O3 -Wall test.cpp -std=c++11 -lrdkafka -lpthread -lz -lstdc++ -lcppkafka

Related

wxWidgets minimal.cpp liner error in function `wxString::ImplStr(char const*, wxMBConv const&)'

Getting this error when compiling and linking the minimal.cpp file from the samples directory
g++ -o "SampleMinimal" ./src/minimal.o -lwx_mswu_xrc-3.0-x86_64-w64-mingw32 -lwx_mswu_webview-3.0-x86_64-w64-mingw32 -lwx_mswu_html-3.0-x86_64-w64-mingw32 -lwx_mswu_qa-3.0-x86_64-w64-mingw32 -lwx_mswu_adv-3.0-x86_64-w64-mingw32 -lwx_mswu_core-3.0-x86_64-w64-mingw32 -lwx_baseu_xml-3.0-x86_64-w64-mingw32 -lwx_baseu_net-3.0-x86_64-w64-mingw32 -lwx_baseu-3.0-x86_64-w64-mingw32 -L/usr/x86_64-w64-mingw32/sys-root/mingw/lib -Wl,--subsystem,windows -mwindows -Wl,--enable-auto-import
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: ./src/minimal.o: in function `wxString::ImplStr(char const*, wxMBConv const&)':
/usr/x86_64-w64-mingw32/sys-root/mingw/include/wx-3.0/wx/string.h:477: undefined reference to `wxString::ConvertStr(char const*, unsigned long, wxMBConv const&)'
/usr/x86_64-w64-mingw32/sys-root/mingw/include/wx-3.0/wx/string.h:477:(.text$_ZN8wxString7ImplStrEPKcRK8wxMBConv[_ZN8wxString7ImplStrEPKcRK8wxMBConv]+0x3a): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `wxString::ConvertStr(char const*, unsigned long, wxMBConv const&)'
collect2: error: ld returned 1 exit status
Successfully creating object file.
Compiler command line:
g++ -D_FILE_OFFSET_BITS=64 -DWXUSINGDLL -D__WXMSW__ -I/usr/x86_64-w64-mingw32/sys-root/mingw/lib/wx/include/x86_64-w64-mingw32-msw-unicode-3.0 -I"/usr/x86_64-w64-mingw32/sys-root/mingw/include/wx-3.0" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/minimal.d" -MT"src/minimal.o" -o "src/minimal.o" "../src/minimal.cpp"
Finished building: ../src/minimal.cpp
Linker command line:
g++ -o "SampleMinimal" ./src/minimal.o -lwx_mswu_xrc-3.0-x86_64-w64-mingw32 -lwx_mswu_webview-3.0-x86_64-w64-mingw32 -lwx_mswu_html-3.0-x86_64-w64-mingw32 -lwx_mswu_qa-3.0-x86_64-w64-mingw32 -lwx_mswu_adv-3.0-x86_64-w64-mingw32 -lwx_mswu_core-3.0-x86_64-w64-mingw32 -lwx_baseu_xml-3.0-x86_64-w64-mingw32 -lwx_baseu_net-3.0-x86_64-w64-mingw32 -lwx_baseu-3.0-x86_64-w64-mingw32 -L/usr/x86_64-w64-mingw32/sys-root/mingw/lib -Wl,--subsystem,windows -mwindows -Wl,--enable-auto-import
You're apparently compiling minimal sample in debug mode, see -g3 flag, then attempting to link against release mswu libs.
So either change the sample compilation to release mode, or change the linker command to use mswud debug libs.
Solved the problem by making Command: x86_64-w64-mingw32-g++ for both the GCC C++ compiler and the MinGW C++ Linker in the C/C++ build settings in Eclipse. I thought this was done automatically by selecting the MinGW tool chain. Many thanks to Catalin for pointing out that I was linking against release libraries in the debug build configuration. His answer to make my build configuration release got me that one step further. Thanks!

g++ - linking stage (-L flag) not working

I am new to C++ and am trying to figure out where in the compiling process my error is. Apologies, if this question is unclear, I'm not sure what information to provide.
Reference information: The directory "mbedtls/lib/" contains two .a (archive) files, "libmbedtls_SGX_t.a" and "libmbedtls_SGX_u.a".
I am using a Makefile, which runs the following commands in order. The commands seem to run error free:
cc -m64 -O2 -fPIC -Wno-attributes -IApp -I/opt/intel/sgxsdk/include -Imbedtls/include -DNDEBUG -UEDEBUG -UDEBUG -c App/Enclave_u.c -o App/Enclave_u.o
g++ -m64 -O2 -fPIC -Wno-attributes -IApp -I/opt/intel/sgxsdk/include -Imbedtls/include -DNDEBUG -UEDEBUG -UDEBUG -std=c++11 -c App/App.cpp -o App/App.o
g++ -m64 -O2 -fPIC -Wno-attributes -IApp -I/opt/intel/sgxsdk/include -Imbedtls/include -DNDEBUG -UEDEBUG -UDEBUG -std=c++11 -c App/sgx_utils/sgx_utils.cpp -o App/sgx_utils/sgx_utils.o
After running those commands, it runs this command:
g++ App/Enclave_u.o App/App.o App/sgx_utils/sgx_utils.o -o app -m64 -O2 -L/opt/intel/sgxsdk/lib64 -lsgx_urts_sim -lpthread -Lmbedtls/lib/ -lsgx_uae_service_sim
However, this command produces an error:
App/Enclave_u.o: In function `Enclave_ocall_print_string':
Enclave_u.c:(.text+0x9): undefined reference to `ocall_print_string'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_free':
Enclave_u.c:(.text+0x28): undefined reference to `ocall_mbedtls_net_free'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_recv_timeout':
Enclave_u.c:(.text+0x54): undefined reference to `ocall_mbedtls_net_recv_timeout'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_send':
Enclave_u.c:(.text+0x71): undefined reference to `ocall_mbedtls_net_send'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_recv':
Enclave_u.c:(.text+0x91): undefined reference to `ocall_mbedtls_net_recv'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_usleep':
Enclave_u.c:(.text+0xa8): undefined reference to `ocall_mbedtls_net_usleep'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_set_nonblock':
Enclave_u.c:(.text+0xc9): undefined reference to `ocall_mbedtls_net_set_nonblock'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_set_block':
Enclave_u.c:(.text+0xe9): undefined reference to `ocall_mbedtls_net_set_block'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_accept':
Enclave_u.c:(.text+0x119): undefined reference to `ocall_mbedtls_net_accept'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_bind':
Enclave_u.c:(.text+0x144): undefined reference to `ocall_mbedtls_net_bind'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_connect':
Enclave_u.c:(.text+0x164): undefined reference to `ocall_mbedtls_net_connect'
collect2: error: ld returned 1 exit status
Makefile:184: recipe for target 'app' failed
I don't know why this error is being produced. My last command includes the flag -Lmbedtls/lib/ which should resolve these undefined references by linking the archive files (specifically - libmbedtls_SGX_u.a) to the executable. However, clearly this is not happening.
Do I need to link the archive files earlier in the compile process? I.e - instead of using the flag -Lmbedtls/lib/ in the final g++ command, should I be using it in both of the previous g++ commands?
Update: Adding the flag -Lmbedtls/lib/ to the previous 2 g++ commands (the ones that generated App.o and sgx_utils.o) did not change anything. The error remains.
As has been mentioned in the comment, only specifying a directory with -L without specifying the name of a library in that directory is useless.
So you have to additionally specify the library names with -l.
So instead of:
g++ App/Enclave_u.o App/App.o App/sgx_utils/sgx_utils.o -o app -m64 -O2 \
-L/opt/intel/sgxsdk/lib64 -lsgx_urts_sim -lpthread \
-Lmbedtls/lib/ \
-lsgx_uae_service_sim
Use:
g++ App/Enclave_u.o App/App.o App/sgx_utils/sgx_utils.o -o app -m64 -O2 \
-L/opt/intel/sgxsdk/lib64 -lsgx_urts_sim -lpthread \
-Lmbedtls/lib/ -lmbedtls_SGX_t -lmbedtls_SGX_u \
-lsgx_uae_service_sim

undefined reference to boost library

I am getting static link errors to boost's non header only library. Let's solve it together:
The final binary is supposed to be built using libraries(libbasemainif.a for example) separately built as the higher layers. One of those layers, uses boost::filesystem.
Have a look at the simplified vesrion of g++ command:
g++ /mylis/1.a /mylibs/2.a
-L/myboost/Linux-x86_64/lib64
-Wl,-rpath,/myboost/Linux-x86_64/lib64 -Wl,-Bstatic
-lboost_thread-mt -lboost_system-mt -lboost_filesystem-mt -lboost_date_time-mt
-Wl,-Bdynamic
-Wl,-rpath,/myinstall/usr/local/lib64 -L/myinstall/usr/local/lib64 -Wl,
-Bstatic -lmyblahblah-static -Wl,-Bdynamic **-lbasemainif** -lbaseif -ldl -rdynamic -lz -lrt
-L/mypackage1/Linux-x86_64/debug/lib -L /mypackage2/18.1/Linux-x86_64/debug/lib -lpthread -Wl,-rpath,$ORIGIN/../lib64
and this is the error :
/blahblah/lib/libbasemainif.a(errorreportfile.o):
In function `boost::filesystem3::remove(boost::filesystem3::path const&, boost::system::error_code&)':
/myboost/Linux-x86_64/include/boost/filesystem/v3/operations.hpp:411:
undefined reference to boost::filesystem3::detail::remove(boost::filesystem3::path const&, boost::system::error_code*)'
collect2: ld returned 1 exit status
If I remove -Wl,-Bstatic the g++ command executes successfully.But this is not an option in production.
The libboost_filesystem.a is available. And all of the laibraries in every layer used boost from the same location.
Can you please tell me why I am getting this error? Thank you
You should pass to gcc library that implements function after the piece which references it. Something like: -lbasemainif -lboost_filesystem-mt. This is how gcc linker resolves dependencies.
Here is great detailed explanation.

Failure linking when using an extra library

When I build a simple C++ program with glog library (Google's logging library which is not used in the code), I get "undefined reference" errors. When I remove the -lglog from the build command, the link succeeds.
Notice that the library that I added to the link is not used in the code at all and despite that it caused the build to fail. In addition, the glog and log4cpp libraries are supposed to be independent.
Can you explain this unusual behavior?
Environment: Ubuntu 14.04
Code:
//test.cpp
#include "log4cpp/Appender.hh"
#include "log4cpp/FileAppender.hh"
int main() {
log4cpp::Appender *appender;
appender = new log4cpp::FileAppender("default", "program.log");
return 0;
}
Working build command:
$ g++ test.cpp -llog4cpp -lpthread
Failing build command:
$ g++ test.cpp -llog4cpp -lglog -lpthread
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_key_create'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_getspecific'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_key_delete'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status
EDIT:
This command also builds successfully:
g++ test.cpp -llog4cpp -lpthread -lglog
This command fails (change the order of libs):
$ g++ test.cpp -llog4cpp -lglog -lpthread
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_key_create'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_getspecific'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_key_delete'
//usr/local/lib/liblog4cpp.so: undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status
This succeeds:
$ g++ test.cpp -pthread -llog4cpp
This fails:
$ g++ test.cpp -pthread -llog4cpp -lglog
EDIT 2:
I studied the duplicate suggestions (1) and (2) to find out maybe there's something useful to me there, but it turned out irrelevant because these cases doesn't address the situation where a library that is not used in the code is added to the link and make it fail.
EDIT 3:
The files from my environment (glog libs, log4cpp libs, used log4cpp headers and test.cpp): log_test.zip.

Undefined reference to 'dlsym' and 'dlopen'

I am compiling using arm-linux-gnueabi-g++ version 4.7.3.
I have the arm-linux-gnueabi libraries installed at location:
/usr/arm-linux-gnueabi/lib, it contains libdl.a, libdl.so, libdl.so.2,
and libdl-2.19.so.
libdl.so links to libdl.so.2 which links to libdl-2.19.so.
I am trying to link against the dl library (see command string below), but I always get the undefined reference errors.
arm-linux-gnueabi-g++ -I. -I../ -I../Comms/Linux -Wall -DLINUX -fpic -o ../../work/MyProgram main.o
-L../../work -L/usr/arm-linux-gnueabi/lib -lComms -lConsole -lUtilities -ldl
../../work/libUtilities.so: undefined reference to `dlsym'
../../work/libUtilities.so: undefined reference to `dlopen'
collect2: error: ld returned 1 exit status
If I compile using g++ 4.8.2 using the following commend then my program compiles, links, and executes fine.
g++ -I. -I../ -I../Comms/Linux -Wall -DLINUX -fpic -o ../../work/MyProgram main.o
-L../../work -lComms -lConsole -lUtilities -ldl
Obviously it can't find the libdl.so library; I thought that by adding the path to the location of the appropriate library by using the -L flag would fix the problem, but it didn't.
What am I missing with the ARM compiler command?
Well, I found the answer, I needed -Wl,--no-as-needed flag before the -ldl. I had run across this flag before I asked the question, but apparently mistyped it because it hadn't worked for me.
I don't understand why the flag is needed, but the code does finish linking now.
A SO user here says that it has to do with recent (2013 as of the user's post) versions of gcc linking to --as-needed.