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

linker error while compiling example cpp program #695

Closed
captKBK opened this issue Feb 1, 2024 · 18 comments
Closed

linker error while compiling example cpp program #695

captKBK opened this issue Feb 1, 2024 · 18 comments

Comments

@captKBK
Copy link

captKBK commented Feb 1, 2024

Hi,

I am facing an issue while compiling the example cpp program(under this section: https://github.com/jupp0r/prometheus-cpp/tree/v1.0.1?tab=readme-ov-file#usage),

I am using v1.0.1, c++11, CentOS 7.9, GCC 11.2.0

I have used "-lprometheus-cpp-pull -lprometheus-cpp-core -lz" but still getting undefined reference error for Exposer methods.

Error for reference:
->cmd line command:
g++ -std=c++11 -o prom.exe test_prometheus.cpp -I/root/prometheus_poc/prometheus_libs/include -L/root/prometheus_poc/prometheus_libs/lib64/ -lprometheus-cpp-pull -lprometheus-cpp-core -lz

Error:
/tmp/ccXC71MO.o: In function main': test_prometheus.cpp:(.text+0x77): undefined reference to prometheus::Exposer::Exposer(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, unsigned long, CivetCallbacks const*)'
test_prometheus.cpp:(.text+0xf6): undefined reference to prometheus::detail::Builder<prometheus::Counter>::Name(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' test_prometheus.cpp:(.text+0x135): undefined reference to prometheus::detail::Builderprometheus::Counter::Help(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&)'
test_prometheus.cpp:(.text+0x5b6): undefined reference to prometheus::detail::Builder<prometheus::Counter>::Name(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' test_prometheus.cpp:(.text+0x5f5): undefined reference to prometheus::detail::Builderprometheus::Counter::Help(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&)'
test_prometheus.cpp:(.text+0x6c4): undefined reference to prometheus::Exposer::RegisterCollectable(std::weak_ptr<prometheus::Collectable> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' /tmp/ccXC71MO.o: In function prometheus::Counter& prometheus::Familyprometheus::Counter::Add<>(std::map<std::__cxx11::basic_string<char, std::char_traits, std::allocator >, std::__cxx11::basic_string<char, std::char_traits, std::allocator >, std::less<std::__cxx11::basic_string<char, std::char_traits, std::allocator > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits, std::allocator > const, std::__cxx11::basic_string<char, std::char_traits, std::allocator > > > > const&)':
test_prometheus.cpp:(.text.ZN10prometheus6FamilyINS_7CounterEE3AddIJEEERS1_RKSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESB_St4lessISB_ESaISt4pairIKSB_SB_EEEDpOT[ZN10prometheus6FamilyINS_7CounterEE3AddIJEEERS1_RKSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESB_St4lessISB_ESaISt4pairIKSB_SB_EEEDpOT]+0x30): undefined reference to `prometheus::Familyprometheus::Counter::Add(std::map<std::__cxx11::basic_string<char, std::char_traits, std::allocator >, std::__cxx11::basic_string<char, std::char_traits, std::allocator >, std::less<std::__cxx11::basic_string<char, std::char_traits, std::allocator > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits, std::allocator > const, std::__cxx11::basic_string<char, std::char_traits, std::allocator > > > > const&, std::unique_ptr<prometheus::Counter, std::default_deleteprometheus::Counter >)'
collect2: error: ld returned 1 exit status

@kloczek
Copy link

kloczek commented Feb 4, 2024

This is not about example programs but when source code is configured with -D ENABLE_TESTING=ON
Have the same issue:

[ 98%] Linking CXX executable ../../../bin/prometheus_push_internal_test
cd /home/tkloczko/rpmbuild/BUILD/prometheus-cpp-1.2.1/x86_64-redhat-linux-gnu/push/tests/internal && /usr/bin/cmake -E cmake_link_script CMakeFiles/prometheus_push_internal_test.dir/link.txt --verbose=1
/usr/bin/g++ -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -O2 -g -grecord-gcc-switches -pipe -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -fdata-sections -ffunction-sections -fstack-protector-strong -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -flto=auto -Wp,-U_FORTIFY_SOURCE,-D_FORTIFY_SOURCE=3 -Wp,-D_GLIBCXX_ASSERTIONS -Wall -Werror=format-security -fno-rtti -DNDEBUG -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -Wl,--gc-sections -Wl,--as-needed -Wl,--build-id=sha1 -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-z,pack-relative-relocs -flto=auto -fuse-linker-plugin CMakeFiles/prometheus_push_internal_test.dir/label_encoder_test.cc.o -o ../../../bin/prometheus_push_internal_test  -Wl,-rpath,/home/tkloczko/rpmbuild/BUILD/prometheus-cpp-1.2.1/x86_64-redhat-linux-gnu/lib /usr/lib64/libgmock_main.so.1.14.0 ../../../lib/libprometheus-cpp-push.so.1.2.0 ../../../lib/libprometheus-cpp-core.so.1.2.0 /usr/lib64/libgmock.so.1.14.0 /usr/lib64/libgtest.so.1.14.0
/usr/bin/ld: /tmp/ccepa2St.ltrans0.ltrans.o: in function `prometheus::(anonymous namespace)::LabelEncoderTest_unicode_Test::TestBody()':
/home/tkloczko/rpmbuild/BUILD/prometheus-cpp-1.2.1/push/tests/internal/label_encoder_test.cc:15:(.text._ZN10prometheus12_GLOBAL__N_129LabelEncoderTest_unicode_Test8TestBodyEv+0x274): undefined reference to `prometheus::detail::encodeLabel(std::ostream&, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&)'
/usr/bin/ld: /tmp/ccepa2St.ltrans0.ltrans.o: in function `prometheus::(anonymous namespace)::LabelEncoderTest_path_Test::TestBody()':
/home/tkloczko/rpmbuild/BUILD/prometheus-cpp-1.2.1/push/tests/internal/label_encoder_test.cc:15:(.text._ZN10prometheus12_GLOBAL__N_126LabelEncoderTest_path_Test8TestBodyEv+0x237): undefined reference to `prometheus::detail::encodeLabel(std::ostream&, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&)'
/usr/bin/ld: /tmp/ccepa2St.ltrans0.ltrans.o: in function `prometheus::(anonymous namespace)::LabelEncoderTest_empty_Test::TestBody()':
/home/tkloczko/rpmbuild/BUILD/prometheus-cpp-1.2.1/push/tests/internal/label_encoder_test.cc:15:(.text._ZN10prometheus12_GLOBAL__N_127LabelEncoderTest_empty_Test8TestBodyEv+0x226): undefined reference to `prometheus::detail::encodeLabel(std::ostream&, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&)'
/usr/bin/ld: /tmp/ccepa2St.ltrans0.ltrans.o: in function `prometheus::(anonymous namespace)::LabelEncoderTest_regular_Test::TestBody()':
/home/tkloczko/rpmbuild/BUILD/prometheus-cpp-1.2.1/push/tests/internal/label_encoder_test.cc:15:(.text._ZN10prometheus12_GLOBAL__N_129LabelEncoderTest_regular_Test8TestBodyEv+0x239): undefined reference to `prometheus::detail::encodeLabel(std::ostream&, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&)'
collect2: error: ld returned 1 exit status
make[2]: *** [push/tests/internal/CMakeFiles/prometheus_push_internal_test.dir/build.make:105: bin/prometheus_push_internal_test] Error 1

@captKBK
Copy link
Author

captKBK commented Feb 5, 2024

In my case I had disabled the testing -DENABLE_TESTING=OFF and the source got compiled well and has generated the libs, not sure why the example code is not able to compile and displaying undefined reference error

@kloczek
Copy link

kloczek commented Feb 5, 2024

In my case I had disabled the testing -DENABLE_TESTING=OFF and the source got compiled well and has generated the libs, not sure why the example code is not able to compile and displaying undefined reference error

But this is nothing more than just swiping dust under the carpet 😋

@gjasny
Copy link
Collaborator

gjasny commented Feb 5, 2024

@kloczek Your issue is a different one and fixed with v1.2.1 (see #687)

@gjasny
Copy link
Collaborator

gjasny commented Feb 5, 2024

@captKBK could you please run:

nm -C /root/prometheus_poc/prometheus_libs/lib64/libprometheus-cpp-pull.a | grep 'Exposer::Exposer'

to see the defined symbols? Your test_prometheus.cpp wants a std::__cxx11::basic_string string (note the __cxx11). I wonder what the prometheus-cpp-pull lib defines.

@gjasny
Copy link
Collaborator

gjasny commented Feb 5, 2024

Did you try the latest 1.2.1 version?

@captKBK
Copy link
Author

captKBK commented Feb 6, 2024

Hi @gjasny
I tried the above command

Here is the output,
I have generated shared libraries files,

[root@localhost cppApp]# nm -C /root/prometheus_poc/prometheus_libs/lib64/libprometheus-cpp-pull.so | grep 'Exposer::Exposer'
000000000002f00e T prometheus::Exposer::Exposer(std::string const&, unsigned long, CivetCallbacks const*)
000000000002f1ce T prometheus::Exposer::Exposer(std::vector<std::string, std::allocatorstd::string >, CivetCallbacks const*)
000000000002f00e T prometheus::Exposer::Exposer(std::string const&, unsigned long, CivetCallbacks const*)
000000000002f1ce T prometheus::Exposer::Exposer(std::vector<std::string, std::allocatorstd::string >, CivetCallbacks const*)

@kloczek
Copy link

kloczek commented Feb 6, 2024

Did you try the latest 1.2.1 version?

If you will look one more time on output which I've provided you can find that it was generated on building exactly that version.

@gjasny
Copy link
Collaborator

gjasny commented Feb 6, 2024

@captKBK It looks like your prometheus-cpp-pull lib was compiled with _GLIBCXX_USE_CXX11_ABI=0 and your test case with _GLIBCXX_USE_CXX11_ABI=1. To be fully sure please omit the -C argument from the nm command and also use nm on test_prometheus.o. If the defined and used Exposer::Exposer constructor differ in the unmangled form we know for sure that _GLIBCXX_USE_CXX11_ABI is the culprit.

Theoretically the error will vanish if you run:

scl enable devtolset-11 -- g++ -std=c++11 -o prom.exe test_prometheus.cpp -I/root/prometheus_poc/prometheus_libs/include -L/root/prometheus_poc/prometheus_libs/lib64/ -lprometheus-cpp-pull -lprometheus-cpp-core -lz

@gjasny
Copy link
Collaborator

gjasny commented Feb 6, 2024

Are you using GCC 11 from devtoolset-11?

@kloczek
Copy link

kloczek commented Feb 6, 2024

Are you using GCC 11 from devtoolset-11?

I'm using devel snapshot of the gcc 14,x which is now available in fedora rawhide.

@gjasny
Copy link
Collaborator

gjasny commented Feb 6, 2024

@kloczek and your distribution is also fedora rawhide?

@gjasny
Copy link
Collaborator

gjasny commented Feb 6, 2024

@kloczek Please test v1.2.2. I forgot to release a new version with the build fix. Sorry about that.

@kloczek
Copy link

kloczek commented Feb 6, 2024

Just tested 1.2.2 and now all looks good and it passes test suite as well😄

+ cd prometheus-cpp-1.2.2
+ /usr/bin/ctest --test-dir x86_64-redhat-linux-gnu --output-on-failure --force-new-ctest-process -j48
Internal ctest changing into directory: /home/tkloczko/rpmbuild/BUILD/prometheus-cpp-1.2.2/x86_64-redhat-linux-gnu
Test project /home/tkloczko/rpmbuild/BUILD/prometheus-cpp-1.2.2/x86_64-redhat-linux-gnu
    Start 1: prometheus_core_test
    Start 2: prometheus_util_test
    Start 3: prometheus_pull_integration_test
    Start 4: prometheus_pull_test
    Start 5: prometheus_push_internal_test
1/5 Test #2: prometheus_util_test ...............   Passed    0.01 sec
2/5 Test #5: prometheus_push_internal_test ......   Passed    0.01 sec
3/5 Test #1: prometheus_core_test ...............   Passed    1.30 sec
4/5 Test #4: prometheus_pull_test ...............   Passed    2.01 sec
5/5 Test #3: prometheus_pull_integration_test ...   Passed   28.12 sec

100% tests passed, 0 tests failed out of 5

@kloczek Please test v1.2.2. I forgot to release a new version with the build fix. Sorry about that.

Thing happens .. you don't need to say sorry as your rection was instant 😋

Thank you very much👍

@captKBK
Copy link
Author

captKBK commented Feb 7, 2024

Are you using GCC 11 from devtoolset-11?

@gjasny No I have compiled it from source on my machine

@kloczek
Copy link

kloczek commented Feb 7, 2024

@kloczek and your distribution is also fedora rawhide?

Almost 100% in case of most of the packages however I'm still I need to finish clean gcc so this is why I'm using gcc from rawhide.

@gjasny
Copy link
Collaborator

gjasny commented Feb 7, 2024

@captKBK you probably compiled prometheus-cpp with a different compiler than you used for your test_prometheus.cpp. If you use the same one the error will vanish.

@captKBK
Copy link
Author

captKBK commented Feb 8, 2024

@gjasny Yes that worked, I found root cause, gcc 4.8 was still existing in my machine and while compiling prometheus-cpp it was considering gcc 4.8 and not 11.2, I removed it and recompiled the prometheus-cpp source, it worked fine. Thanks for the help!

@captKBK captKBK closed this as completed Feb 8, 2024
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

3 participants