Search code examples
c++linkerheadernamespacesundefined-reference

Undefined reference to linker error when using namespaces in headers in c++


I've read through all similar "Undefined reference to" threads I could find but couldn't find a solution. Most of the other threads also involved classes which I don't plan to use for this. The program compiles executes normally if I define the function within the header instead of using an external .cc file. I feel like I'm missing something simple here.

This is the simplest test I could put together that recreates the issue I'm having.

Compiler: g++ (Debian 8.3.0-6) 8.3.0

hntest.h

namespace hntest
{
    void pewpew();
}

hntest.cc

#include <iostream>
#include "hntest.h"

namespace hntest
{
    void pewpew()
    {
        std::cout << "pew pew pew!!!" << std::endl;
    }
}

hntestmain.cc

#include "hntest.h"

int main(int argc, char* argv[])
{
    hntest::pewpew();
}

I'm attempting to compile with:

g++ -lstdc++ hntestmain.cc -o hntestmain

And I get the following linker error:

hntestmain.cc:(.text+0x10): undefined reference to `hntest::pewpew()'
collect2: error: ld returned 1 exit status

I have tried reading through the code of a couple popular C++ libraries as well as some of my own older C (not ++) code and makefiles but haven't been able to find my error. I'm admittedly both and amateur an a bit rusty.

What am I missing?


Solution

  • You are not actually compiling the cpp file that has the definition of pewpew.

    Try:

    g++ -lstdc++ hntestmain.cc hntest.cc -o hntestmain
    

    The compiler needs to know about all the source files. The header file is dealt with during pre-process and knows to look in the same folder. You can imagine that if you have more files, say 10, 100 or 10000, this would become impossible to manage by using command line. That is why people created build systems like make, cmake and bazel.

    For much greater detail see the answer here which is specific for your case of linker error.