I have a static library test1.a
built on linux. When I am trying to link against it, the linker throws undefined reference errors:
CMakeFiles/Testserver.dir/src/audio/TestEncoderFactory.cpp.o: In function
TestEncoderFactory::GetSupportedEncoders()
: TestH264BypassedEncoder.cpp:(.text._ZN10H264BypassedEncoderC2ERKN7cricket10VideoCodecERNS_14TestClientE+0x191):undefined reference to absl::EqualsIgnoreCase(std::basic_string_view<char, std::char_traits<char> >, std::basic_string_view<char, std::char_traits<char> >)
TestEncoderFactory.cpp:(.text._ZN10TestEncoderFactory20GetSupportedEncodersEv+0x20b): undefined reference to `test::SdpAudioFormat::SdpAudioFormat(std::basic_string_view<char, std::char_traits >, int, unsigned long, 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 > > > >&&)'
But then I created a sample program using the same line of code leading to linker errors as shown below and when I build it by linking against the same test.a
the executable is building perfectly fine:
#include <string>
#include <rtc_base/logging.h>
#include <rtc_base/checks.h>
#include <iostream>
#include <absl/strings/match.h>
#include <memory>
using namespace std;
int main() {
cout << "testing\n";
RTC_CHECK(absl::EqualsIgnoreCase("", ""));
//RTC_LOG(INFO) << "Testing libwebrtc linking";
bool res = absl::EqualsIgnoreCase("ab", "");
cout << res;
cout << "\nending";
}
I built above using g++ -I./include -I./include/third_party/abseil-cpp test.cpp -L./lib -ltest1 -o final_ex
the executable final_ex
builds successfully.
The other program is a bigger one(and is private and thus can't share) and trying to link against test1.a
and other are .so
as mentioned in the command below:
/usr/bin/g++ -pthread -g -rdynamic CMakeFiles/testServer.dir/src/Main.cpp.o testxyz.cpp.o -o bin/testServer -L<lib_loc> -Wl,-rpath,<path....> -static-libstdc++ -static-libgcc -ltest1 -lprotobuf -lgio-2.0 -lgobject-2.0 -lglib-2.0 -ljson-glib-1.0 -lffmpeg -lX11 -ldl
I also checked many different questions on Stackoverflow but couldn't fix the issue. Can linking against static and dynamic libs be the cause behind the issue?
Fix
As question is closed so adding the fix details in question itself.
One important detail I forgot to mention was I used cxx_std_17
to build my code. Also I was not aware that the static lib test1.a was built using cxx_std_14
. Once I got to know this I used cxx_std_14
and all the linker errors were resolved.
I didn't think about it earlier as I was of the understanding that it's safe to link C++17
and C++14
as mentioned in answer to this stackoverflow question. But found the hard way that it may not be the case.