Reputation: 399
I have a "small" graphics library I'm building, and I'm running into this error when attempting to build a test binary that links against it:
al_test.o: In function `test_wave()':
al_test.cpp:(.text+0x1a7): undefined reference to `min::wave::wave(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
...
(and other, similar errors, but we'll focus on this one). The library is built in a makefile with the following rules:
all: libmgl.a libmgl.so
libmgl.so: $(OBJECTS)
$(LD) $* $(LDFLAGS) -shared -o $@
libmgl.a: $(OBJECTS)
$(AR) $(ARFLAGS) libmgl.a $^
%.o: %.cpp
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(INCLUDES) -c -o $@ $<
CXXFLAGS
contains a few things, but most importantly -std=c++14 -O3 -Wall -Werror
and all of the objects compile just fine and the static and dynamic libraries are output without error or warning.
Now I have a file source/test/al_test.cpp
meant to test only the audio portions of the library that looks something like this:
#include <iostream>
#include "twave.h"
int main()
{
try
{
bool out = true;
out = out && test_wave();
// out = out && test_ogg();
// out = out && test_sound_buffer();
if (out)
{
std::cout << "Sound tests passed!" << std::endl;
return 0;
}
}
catch (std::exception &ex)
{
std::cout << ex.what() << std::endl;
}
std::cout << "Sound tests failed!" << std::endl;
return -1;
}
and the function test_wave
is defined in twave.h
like this:
#include <stdexcept>
#include "min/wave.h"
bool test_wave()
{
bool out = true;
// Load invention wav file
{
const min::wave sound = min::wave("data/sound/invention_no_1.wav");
// File should not be mono
out = out && !sound.is_mono();
if (!out)
{
throw std::runtime_error("Failed wave file not is_mono");
}
// Other checks that aren't important...
}
return out;
}
So here's my problem: min::wave::wave()
definitely exists and definitely ought to be in the library. Here's a stripped-down min/wave.h
:
#ifndef __WAVE__
#define __WAVE__
#include <string>
namespace min {
class wave
{
private:
void load(const std::string);
public:
wave(const std::string&);
};
}
#endif
and min/wave.cpp
#include "wave.h"
min::wave::wave(const std::string &file)
{
load(file);
}
// Definition of `load` omitted; I don't think it's important,
// but know that it is here
So the file exists, the function declaration and definition both exist, and I can see in my make
output that wave.o
is being built and linked/archived into libmgl.a
/libmgl.so
. Yet the compiler claims it doesn't exist. It doesn't seem to be a link order problem; here's my process for building the tests:
g++ -c -Imin/ al_test.cpp -o al_test.o
g++ al_test.o -L. -lmgl -lX11 -lGL -lfreetype -lopenal -lvorbisfile -o bin/al_test
mgl
is obviously the name of my library, and those are the other things on which it depends. I also tried replacing -L.
with -L/absolute/path/to/project/directory/
, to no avail.
Why does g++ think my function is undefined?
nm
commandsBy popular demand, here are a few nm
commands and their outputs:
the static library:
nm -C libmgl.a | grep wave
wavefront.o
wave.o:
0000000000000090 T min::wave::load(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)
0000000000000000 W void min::wave::load<min::mem_file>(min::mem_file const&)
0000000000000000 W void min::wave::load<std::vector<unsigned char, std::allocator<unsigned char> > >(std::vector<unsigned char, std::allocator<unsigned char> > const&)
0000000000000000 T min::wave::clear()
0000000000000880 T min::wave::wave(min::mem_file const&)
0000000000000720 T min::wave::wave(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
0000000000000880 T min::wave::wave(min::mem_file const&)
0000000000000720 T min::wave::wave(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
0000000000000080 T min::wave::get_sample_rate() const
0000000000000060 T min::wave::get_data_samples() const
0000000000000050 T min::wave::get_bits_per_sample() const
0000000000000040 T min::wave::data() const
0000000000000020 T min::wave::is_mono() const
0000000000000030 T min::wave::is_stereo() const
0000000000000550 T min::sound_buffer::add_wave_pcm(min::wave const&)
U min::wave::get_sample_rate() const
U min::wave::get_bits_per_sample() const
U min::wave::data() const
U min::wave::is_stereo() const
...the dynamic library:
nm -C libmgl.so
0000000000201020 B __bss_start
0000000000201020 b completed.7631
w __cxa_finalize
0000000000000460 t deregister_tm_clones
00000000000004f0 t __do_global_dtors_aux
0000000000200e88 t __do_global_dtors_aux_fini_array_entry
0000000000201018 d __dso_handle
0000000000200e90 d _DYNAMIC
0000000000201020 D _edata
0000000000201028 B _end
000000000000053c T _fini
0000000000000530 t frame_dummy
0000000000200e80 t __frame_dummy_init_array_entry
0000000000000548 r __FRAME_END__
0000000000201000 d _GLOBAL_OFFSET_TABLE_
w __gmon_start__
0000000000000420 T _init
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
00000000000004a0 t register_tm_clones
0000000000201020 d __TMC_END__
...and the test file:
nm -C al_test.o
U __cxa_allocate_exception
U __cxa_atexit
U __cxa_begin_catch
U __cxa_end_catch
U __cxa_free_exception
U __cxa_throw
U __dso_handle
00000000000000e0 t _GLOBAL__sub_I__Z9test_wavev
U __gxx_personality_v0
0000000000000000 r .LC1
0000000000000000 T main
U __stack_chk_fail
U _Unwind_Resume
0000000000000000 T test_wave()
U operator delete(void*)
U min::wave::wave(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
U min::wave::is_mono() const
U std::runtime_error::runtime_error(char const*)
U std::runtime_error::~runtime_error()
U std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_create(unsigned long&, unsigned long)
U std::ios_base::Init::Init()
U std::ios_base::Init::~Init()
U std::basic_ios<char, std::char_traits<char> >::clear(std::_Ios_Iostate)
U std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long)
U std::cout
U std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)
0000000000000000 b std::__ioinit
U typeinfo for std::runtime_error
U typeinfo for std::exception
Upvotes: 0
Views: 7072
Reputation: 5463
With more information regarding the symbols available in the static and dynamic libraries, I've come to the conclusion the shared library is empty.
It's empty because $*
here expand to empty.
libmgl.so: $(OBJECTS) $(LD) $* $(LDFLAGS) -shared -o $@
If the target name in an explicit rule does not end with a recognized suffix, ‘$*’ is set to the empty string for that rule.
Replacing $*
with $^
like it's used to build libmgl.a should solve your issue.
Upvotes: 2
Reputation: 40070
Since nm libmgl.so | grep wave
returns nothing (see comments), we know at least that the library is not linked (built?) correctly.
Indeed, you should invoke g++
to link it:
$(CXX) $* $(LDFLAGS) -shared -o $@
Once you've done that, as metioned by user UKMonkey:
order of linking matters
So, your binary should be built like this:
g++ -L. -lmgl -lX11 -lGL -lfreetype -lopenal -lvorbisfile -o bin/al_test al_test.o
Upvotes: 1