0

I try to have a simple function which returns the current directory as a string: Utils.h contains :

#ifndef __UTILS__
#define __UTILS__

#pragma once

#include <string>

#include <stdio.h>  /* defines FILENAME_MAX */
#ifdef __linux__    
    #include <unistd.h>
    #define GetCurrentDir getcwd
#else
    #include <direct.h>
    #define GetCurrentDir _getcwd
#endif


namespace
{
    extern std::string _getCurrentDirectory() 
    {
        char cCurrentPath[FILENAME_MAX];
        if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
            return "";

        cCurrentPath[sizeof(cCurrentPath) - 1] = '\0'; /* not really required */

        return std::string(cCurrentPath);
    }
}


#endif // __UTILS__

Despite extern declaration (redundant ?) and the creation of this specified Utils.h file, I still have linking problem:

Error   2   error LNK2005: "char * __cdecl getCurrentDirectory(void)" (?getCurrentDirectory@@YAPADXZ) already defined in MyLibrary.lib(myobj.obj)
Error   3   error LNK1169: one or more multiply defined symbols found   

This problem appears twice, and MyLibrary.lib doesn't even use or includes Utils.h I really don't understand despite of the time I spent on it. Does anyone have an idea ?

Thanks

Quentin Tealrod
  • 309
  • 3
  • 14
  • 2
    Why do you put the code in an include file? Put the code in a cpp-file and only a declaration in the h-file. Putting the code in an include file means that you may only include the file once in the whole project. – Support Ukraine Sep 17 '15 at 16:17
  • 1
    What do you believe extern does? Did you read http://stackoverflow.com/questions/856636/effects-of-the-extern-keyword-on-c-functions? – Werner Henze Sep 17 '15 at 16:25

0 Answers0