Linker can't find function definition in a namespace
Asked Answered
R

1

11

I get this /tmp/ccnL7Yz1.o: In function 'main': main.cpp:(.text+0x70): undefined reference to 'dng::genDungeon()' main.cpp:(.text+0xf0): undefined reference to 'dng::clrDungeon(char**)' collect2: error: ld returned 1 exit status error when I'm trying to compile my program. It worked great before I added namespace functions. I'm compiling it like this: g++ -std=c++11 main.cpp Dungeon.cpp

Dungeon.h

namespace dng {
    char** genDungeon();
    void clrDungeon(char**);

    class Dungeon {
    //Methods and variables
    }
}

Dungeon.cpp

#include "Dungeon.h"

using namespace dng;
char** genDungeon() 
{
    //Stuff
}
void clrDungeon(char** dungeon) 
{
    //Another Stuff
}
/*Implementation of class methods
void Dungeon::genStart(){} -> like this */

main.cpp

#include "Dungeon.h"

int main () 
{
    //Stuff
    auto dungeon = dng::genDungeon();
    //Stuff
    dng::clrDungeon(dungeon);
    return 0;
}

I also tried to make .o files by myself g++ -std=c++11 -c main.cpp g++ -std=c++11 -c Dungeon.cpp and then link them, but got the same error. What can be the problem?

Rajewski answered 7/8, 2015 at 18:42 Comment(4)
You could have searched a bit first: What is an undefined reference/unresolved external symbol error and how do I fix it?Tortoiseshell
I'm voting to reopen this. The original title smells of duplicate, but the question is really about a misunderstanding of using namespace.Backer
@Борис Кот Такие вопросы лучше задавать на ru.stackoverflow.:)Wiry
@zenith I searched a lot but didn't find anything similar. And it seems like your link doesn't provide a solution for my problem.Filemon
W
12

Enclose the function definitions in the namespace dng where they are declared.

#include "Dungeon.h"

namespace dng
{
char** genDungeon() 
{
    //Stuff
}
void clrDungeon(char** dungeon) 
{
    //Another Stuff
}
//...
}

Or use qualified names.

include "Dungeon.h"

using namespace dng;

//...

char** dng::genDungeon() 
{
    //Stuff
}
void dng::clrDungeon(char** dungeon) 
{
    //Another Stuff
}

Otherwise, the functions are defined in the global namespace, and as a result, you declared four functions: two in the namespace dng and another two in the global namespace.

Wiry answered 7/8, 2015 at 18:44 Comment(10)
Answering obvious duplicates encourages to creating them. Do not do this.Rhyolite
@Mateusz Grzejek I am sorry. I have not understood what you said.Wiry
@Mateusz Grzejek Oh, I have understood now.:)Wiry
You don't need using namespace dng; in the second case.Tortoiseshell
@MateuszGrzejek I don't think it is a duplicate.Filemon
@zenith It is unknown what this module contain else.Wiry
This might be helpful for deciding which convention to use. My vote goes for using qualified names.Tortoiseshell
@VladfromMoscow The functions themselves are inside dng:: so everything from dng:: will be automatically available inside the functions.Tortoiseshell
@zenith You have not understood. It is unknown what else follows the function definitions in the module.:)Wiry
@VladfromMoscow Oh you mean that. Instead of "module" you usually call them source files in C++.Tortoiseshell

© 2022 - 2024 — McMap. All rights reserved.