Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic linking doesn't work #95

Closed
ghost opened this issue Apr 3, 2014 · 23 comments
Closed

dynamic linking doesn't work #95

ghost opened this issue Apr 3, 2014 · 23 comments

Comments

@ghost
Copy link

ghost commented Apr 3, 2014

Hello,

If I try to use dynamic link I always get undefined references.

I installed the libraries in /usr/local/lib ane try to compile rdkafka_example.c using

gcc -I/usr/local/include/librdkafka -L/usr/local/lib -lrdkafka rdkafka_example.c

But I get link errors

/tmp/cczYMsjA.o: In function rd_kafka_message_errstr': rdkafka_example.c:(.text+0x31): undefined reference tord_kafka_err2str'
/tmp/cczYMsjA.o: In function logger': rdkafka_example.c:(.text+0x1f0): undefined reference tord_kafka_name'
/tmp/cczYMsjA.o: In function msg_delivered': rdkafka_example.c:(.text+0x263): undefined reference tord_kafka_err2str'
/tmp/cczYMsjA.o: In function msg_consume': rdkafka_example.c:(.text+0x2e7): undefined reference tord_kafka_topic_name'
rdkafka_example.c:(.text+0x357): undefined reference to rd_kafka_topic_name' /tmp/cczYMsjA.o: In functionmetadata_print':
rdkafka_example.c:(.text+0x5ba): undefined reference to rd_kafka_err2str' rdkafka_example.c:(.text+0x6fc): undefined reference tord_kafka_err2str'
/tmp/cczYMsjA.o: In function sig_usr1': rdkafka_example.c:(.text+0x765): undefined reference tord_kafka_dump'
/tmp/cczYMsjA.o: In function main': rdkafka_example.c:(.text+0x7ea): undefined reference tord_kafka_conf_new'
rdkafka_example.c:(.text+0x7f3): undefined reference to rd_kafka_topic_conf_new' rdkafka_example.c:(.text+0x887): undefined reference tord_kafka_conf_set'
.....

@edenhill
Copy link
Contributor

edenhill commented Apr 3, 2014

Hey,

that's weird.

  • is this latest master?
  • What version of gcc and what OS is this on?
  • Try putting -lrdkafka at the end of the gcc argument list.
  • output from ls -la /usr/local/lib | grep rdkafka

Thanks

@ghost
Copy link
Author

ghost commented Apr 3, 2014

putting -lrdkafka at the end of the gcc arguments list works.

@edenhill
Copy link
Contributor

edenhill commented Apr 3, 2014

Great! Some (older) gcc versions are pickier than others on linking order.

@edenhill edenhill closed this as completed Apr 3, 2014
@gvd
Copy link

gvd commented Jun 9, 2014

I'm having some linking issues with version 5d8d4f0

I can only seem to get it to link to the static libraries. Using the dynamic libraries I get a bunch of undefined reference errors. I tried compiling the cpp example using the dynamic libs but this results in the same errors. I compile the example using static libs using:

/usr/bin/c++ -g -O2 -fPIC -Wall -Werror -Wfloat-equal -Wpointer-arith -Wno-non-virtual-dtor -I/home/user/dev/install/kafka/include/librdkafka rdkafka_example.cpp -o rdkafka_example_cpp -g ../src-cpp/librdkafka++.a ../src/librdkafka.a -lpthread -lz -lstdc++

That works fine. When trying to compile the cpp example using the dynamic libs:

/usr/bin/c++ -g -O2 -fPIC -Wall -Werror -Wfloat-equal -Wpointer-arith -Wno-non-virtual-dtor -I/home/user/dev/install/kafka/include/librdkafka rdkafka_example.cpp -L/home/user/dev/install/kafka/lib -o rdkafka_example_cpp -lpthread -lz -lstdc++ -lrdkafka++ -lrdkafka

/tmp/ccrQv7rs.o: In function ExampleEventCb::event_cb(RdKafka::Event&)': /home/user/dev/git/librdkafka/examples/rdkafka_example.cpp:92: undefined reference toRdKafka::err2str(RdKafka::ErrorCode)'
/home/user/dev/git/librdkafka/examples/rdkafka_example.cpp:75: undefined reference to RdKafka::err2str(RdKafka::ErrorCode)' /tmp/ccrQv7rs.o: In functionmain':
/home/user/dev/git/librdkafka/examples/rdkafka_example.cpp:139: undefined reference to RdKafka::Conf::create(RdKafka::Conf::ConfType)' /home/user/dev/git/librdkafka/examples/rdkafka_example.cpp:140: undefined reference toRdKafka::Conf::create(RdKafka::Conf::ConfType)'
/tmp/ccrQv7rs.o: In function std::string::_M_data() const': /usr/include/c++/4.8/bits/basic_string.h:293: undefined reference toRdKafka::Conf::DEBUG_CONTEXTS'
/tmp/ccrQv7rs.o: In function main': /home/user/dev/git/librdkafka/examples/rdkafka_example.cpp:276: undefined reference toRdKafka::version()'
/home/user/dev/git/librdkafka/examples/rdkafka_example.cpp:276: undefined reference to RdKafka::version_str()' /home/user/dev/git/librdkafka/examples/rdkafka_example.cpp:338: undefined reference toRdKafka::Producer::create(RdKafka::Conf_, std::string&)'
/home/user/dev/git/librdkafka/examples/rdkafka_example.cpp:350: undefined reference to RdKafka::Topic::create(RdKafka::Handle_, std::string const&, RdKafka::Conf_, std::string&)' /home/user/dev/git/librdkafka/examples/rdkafka_example.cpp:484: undefined reference to RdKafka::wait_destroyed(int)'
/home/user/dev/git/librdkafka/examples/rdkafka_example.cpp:375: undefined reference toRdKafka::err2str(RdKafka::ErrorCode)' /home/user/dev/git/librdkafka/examples/rdkafka_example.cpp:401: undefined reference to RdKafka::Consumer::create(RdKafka::Conf_, std::string&)'
/home/user/dev/git/librdkafka/examples/rdkafka_example.cpp:413: undefined reference to RdKafka::Topic::create(RdKafka::Handle*, std::string const&, RdKafka::Conf*, std::string&)' /home/user/dev/git/librdkafka/examples/rdkafka_example.cpp:425: undefined reference toRdKafka::err2str(RdKafka::ErrorCode)'
collect2: error: ld returned 1 exit status

Note that /home/user/dev/install/kafka/lib contains the installed libs.

@edenhill
Copy link
Contributor

The linker script for the C library was run on the C++ library, hiding all of the C++ symbols.

Fixed in latest master.

Thanks for reporting this!

@bhanushrestha
Copy link

bhanushrestha commented May 16, 2016

I am still getting same compilation error as above while trying to build with gcc compiler the librdkafka cpp example code.

./Librdkafkaexample.o: In function metadata_print': /home/lenovo/cpp-workspace/LibrdKafkaTest/Debug/../Librdkafkaexample.cpp:91: undefined reference toRdKafka::err2str(RdKafka::ErrorCode)'
/home/lenovo/cpp-workspace/LibrdKafkaTest/Debug/../Librdkafkaexample.cpp:121: undefined reference to RdKafka::err2str(RdKafka::ErrorCode)' ./Librdkafkaexample.o: In functionmain':
/home/lenovo/cpp-workspace/LibrdKafkaTest/Debug/../Librdkafkaexample.cpp:258: undefined reference to RdKafka::Conf::create(RdKafka::Conf::ConfType)' /home/lenovo/cpp-workspace/LibrdKafkaTest/Debug/../Librdkafkaexample.cpp:259: undefined reference toRdKafka::Conf::create(RdKafka::Conf::ConfType)'
/home/lenovo/cpp-workspace/LibrdKafkaTest/Debug/../Librdkafkaexample.cpp:411: undefined reference to RdKafka::get_debug_contexts()' /home/lenovo/cpp-workspace/LibrdKafkaTest/Debug/../Librdkafkaexample.cpp:411: undefined reference toRdKafka::version()'
/home/lenovo/cpp-workspace/LibrdKafkaTest/Debug/../Librdkafkaexample.cpp:411: undefined reference to RdKafka::version_str()' /home/lenovo/cpp-workspace/LibrdKafkaTest/Debug/../Librdkafkaexample.cpp:476: undefined reference toRdKafka::Producer::create(RdKafka::Conf_, std::string&)'
/home/lenovo/cpp-workspace/LibrdKafkaTest/Debug/../Librdkafkaexample.cpp:488: undefined reference to RdKafka::Topic::create(RdKafka::Handle_, std::string const&, RdKafka::Conf_, std::string&)' /home/lenovo/cpp-workspace/LibrdKafkaTest/Debug/../Librdkafkaexample.cpp:513: undefined reference to RdKafka::err2str(RdKafka::ErrorCode)'
/home/lenovo/cpp-workspace/LibrdKafkaTest/Debug/../Librdkafkaexample.cpp:542: undefined reference toRdKafka::Consumer::create(RdKafka::Conf_, std::string&)' /home/lenovo/cpp-workspace/LibrdKafkaTest/Debug/../Librdkafkaexample.cpp:554: undefined reference to RdKafka::Topic::create(RdKafka::Handle*, std::string const&, RdKafka::Conf*, std::string&)'
/home/lenovo/cpp-workspace/LibrdKafkaTest/Debug/../Librdkafkaexample.cpp:566: undefined reference toRdKafka::err2str(RdKafka::ErrorCode)' /home/lenovo/cpp-workspace/LibrdKafkaTest/Debug/../Librdkafkaexample.cpp:602: undefined reference to RdKafka::Producer::create(RdKafka::Conf*, std::string&)'
/home/lenovo/cpp-workspace/LibrdKafkaTest/Debug/../Librdkafkaexample.cpp:615: undefined reference toRdKafka::Topic::create(RdKafka::Handle_, std::string const&, RdKafka::Conf_, std::string&)' /home/lenovo/cpp-workspace/LibrdKafkaTest/Debug/../Librdkafkaexample.cpp:630: undefined reference to RdKafka::err2str(RdKafka::ErrorCode)'
/home/lenovo/cpp-workspace/LibrdKafkaTest/Debug/../Librdkafkaexample.cpp:651: undefined reference toRdKafka::wait_destroyed(int)' ./Librdkafkaexample.o: In function ExampleEventCb::event_cb(RdKafka::Event&)':
/home/lenovo/cpp-workspace/LibrdKafkaTest/Debug/../Librdkafkaexample.cpp:153: undefined reference toRdKafka::err2str(RdKafka::ErrorCode)' /home/lenovo/cpp-workspace/LibrdKafkaTest/Debug/../Librdkafkaexample.cpp:170: undefined reference to RdKafka::err2str(RdKafka::ErrorCode)'

Even though all the libraries are included and the linkers as given in documentation i get this error. Can anyone help?

@edenhill
Copy link
Contributor

Please provide your linking command line.

@bhanushrestha
Copy link

bhanushrestha commented May 17, 2016

@edenhill I used Eclipse IDE that generated this command on console:
g++ -o "LibrdKafkaTest" ./Librdkafkaexample.o -lrdkafka -lpthread -lrt

I set the linker to have -lz as well as per your documentation in the github project but that gave error as :

/usr/bin/ld: cannot find -lz
collect2: error: ld returned 1 exit status

@edenhill
Copy link
Contributor

You dont seem to be linking with the C++ library, try: -lrdkafka++

@bhanushrestha
Copy link

Thanks a ton @edenhill . It worked and now the project is building. Thanks again for the quick response.

@harunkucuksabanoglu
Copy link

Hello,
I run the commands below but gcc gives error: rdkafka.h: No such file or directory
Please help.

./configure --prefix=/home/gateway/kafkalib
make
make install

I created kafka.conf under ld.so.conf.d folder. (content: /home/gateway/kafkalib)
echo $LD_LIBRARY_PATH
/usr/local/lib:/home/gateway/kafkalib/lib

gcc -O3 Wall -lrdkafka -lz -lpthread -lrt kafkaexample.c

@edenhill
Copy link
Contributor

You need to put the libraries at the end of the command line:
gcc -O3 -Wall kafkaexample.c -lrdkafka -lz -lpthread -lrt

@harunkucuksabanoglu
Copy link

The same error occurs again.

@edenhill
Copy link
Contributor

Would you mind pasting the actual error message?

@harunkucuksabanoglu
Copy link

Line 11: #include "rdkafka.h"

Error :
kafkaexample.c:11:21: error: rdkafka.h: No such file or directory

@edenhill
Copy link
Contributor

If you've installed librdkafka to a non-standard location you'll also need to provide the include path to the compiler, e.g.:

gcc -O3 -Wall -I/home/gateway/kafkalib/include/librdkafka kafkaexample.c -lrdkafka -lz -lpthread -lrt

@harunkucuksabanoglu
Copy link

Error :
/usr/bin/ld: cannot find -lrdkafka
collect2: ld returned 1 exit status

@edenhill
Copy link
Contributor

These are pretty basic C compilation/building concepts and are outside the scope of librdkafka open source support.

gcc -O3 -Wall -I/home/gateway/kafkalib/include/librdkafka kafkaexample.c -L/home/gateway/kafkalib/lib -lrdkafka -lz -lpthread -lrt

@harunkucuksabanoglu
Copy link

Final code is like below and it worked successfully.
Thanks for your help and quick response.

gcc -O3 -Wall -L/home/gateway/kafkalib/lib -lrdkafka -lz -lpthread -lrt -I/home/gateway/kafkalib/include/librdkafka kafkaexample.c

@Flexiboy
Copy link

Hi, I have a linking issue with librdkafka and g++. I use g++-10 (I need it for my main program) and I have this error code:
Screen Shot 2021-05-28 at 10 32 08 AM

Can you help me figure out how to solve it ?

PS: I also tried with -lrdkafka++ and it throws the same error.

@edenhill
Copy link
Contributor

You have an incorrect include path: -I/usr/local/include/librdkafka, it should be without the librdkafka part, so: -I/usr/local/include

@Flexiboy
Copy link

Thank you very much, it worked for me 😁. I was stuck on that for a week 😅

@singhgurpreetcs
Copy link

Hi Team,
I am getting below error while compiling producer code using C kafka library.

[root@EC2AMAZ-ROPJ2S kafka2]# gcc -Wall -o producer producer.c -I/usr/bin/include -lrdkafka -lz -lpthread -lrt
producer.c: In function ‘main’:
producer.c:83:35: warning: implicit declaration of function ‘rd_kafka_message_new’; did you mean ‘rd_kafka_queue_new’? [-Wimplicit-function-declaration]
rd_kafka_message_t *rkmessage = rd_kafka_message_new();
^~~~~~~~~~~~~~~~~~~~
rd_kafka_queue_new
producer.c:83:35: warning: initialization of ‘rd_kafka_message_t *’ {aka ‘struct rd_kafka_message_s *’} from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
producer.c:85:3: warning: implicit declaration of function ‘rd_kafka_message_set_topic’; did you mean ‘rd_kafka_message_status’? [-Wimplicit-function-declaration]
rd_kafka_message_set_topic(rkmessage, topic);
^~~~~~~~~~~~~~~~~~~~~~~~~~
rd_kafka_message_status
producer.c:87:3: warning: implicit declaration of function ‘rd_kafka_message_set_payload’; did you mean ‘rd_kafka_message_set_headers’? [-Wimplicit-function-declaration]
rd_kafka_message_set_payload(rkmessage, (void *) messages[i], message_lens[i]);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
rd_kafka_message_set_headers
/tmp/cckqSz4V.o: In function main': producer.c:(.text+0x21a): undefined reference to rd_kafka_message_new'
producer.c:(.text+0x238): undefined reference to rd_kafka_message_set_topic' producer.c:(.text+0x266): undefined reference to rd_kafka_message_set_payload'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants