0

I'm trying to built a dll file with Microsoft Visual Studio 2015 using C++. I do not use /clr nor do I use precompiled headers. I have to include two own header and c++ files (e.g. model.h and cpp and landscape.h and cpp). Both files include the same own header files (e.g. functions1.h, functions2.h). model.h and landscape.h are included in the dll.h, the corresponding cpp files are included as resource files in the project. When I compile this, I get Linker Errors because the methods of the included header files from function1.h and function2.h are included more than once. However, if I include these header files only in the dll.h file I get compiler errors because the methods defined in these header files are not known in the model.h and landscape.h. How I understand this error is that each resource file is compiled to an obj file and the linker finds the same methods in both obj files, which gives an error. However I cannot figure out how to solve this problem. I would be happy for every help. Cheers Joachim

Hmm...maybe I did not make my Problem clear. I have one header and cpp file for the dll project

    //dllproject.h
#ifdef DLLFUNCTIONS
#define DLL_EXPORTS __declspec(dllexport)
#else
#define DLL_EXPORTS __declspec(dllimport)
#endif

#include <model.h>
#include <landscape.h>

using namespace std;

extern "C"
{
    namespace dll_Project
    {
        //export functions
    }
}

and here comes the corresponding .cpp file

//dllproject.cpp
#include "dllproject.h"

using namespace std;
extern "C"
{
    namespace dllproject
    {
        //export functions
    }
}

the included model.h and landscape.h are own projects in which different classes, structs and functions are difined and which include further header files with various functions. In model.h, landscape.h is included, but model.h is not in landscape.h

//model.h
#pragma once
#include <myfunctions1.h>
#include <myfunctions2.h>
#include <landscape.h>

//declarations of classes and methods

and the corresponding cpp file

//model.cpp
#include <model.h>

//definitions of the declared methods

similar is the landscape.h file

//landscape.h
#pragma once
#include <myfunctions1.h>
#include <myfunctions2.h>

//declaration of multiple classes and methods

and the corresponding cpp file:

//landscape.cpp
#include <landscape.h>
//definitions of the declared methods

myfunctions1.h and myfunctions2.h are only header files in which various different functions are declared and defined.

landscape.h and model.h are found by MVS2015 in the folder external dependencies and can be included. If I compile, the method definitions in the cpp files are not found. If I add them as resource files, the methods are found, but a linker error occurs saying that the functions from myfunctions1.h and myfunctions2.h are defined in model.obj and landscape.obj.

I would be really happy if someone could help me with that. Cheers Joachim

JKlein
  • 31
  • 8
  • Sorry I forgot to write it here, I used #pragma once in all header files (myfunctions1.h, myfunctions2.h, landscape.h and model.h). However #prama once gards afaik only from including one header several times in one object. The problem here is that myfunctions1.h and myfunctions2.h are included in two different objects (model.obj and landscape.obj). When these objects are linked the functions in myfunctions1.h and myfunctions2.h are multiply defined. – JKlein Feb 04 '16 at 07:17
  • Are these functions declared in different namespaces? – Niall Feb 04 '16 at 07:19
  • no own namespaces are used (only in the dllproject.h/cpp) – JKlein Feb 04 '16 at 07:20
  • Are the functions in declared and defined in myfunctions1.h and myfunctions2.h declare with an `inline`? – Niall Feb 04 '16 at 07:28
  • @Niall no they are not. Just regulare functions like e.g. `double myFunktion(string input_string)` – JKlein Feb 04 '16 at 07:36
  • Yes thanks a lot! It helped me with most of the linker errors. Unfortunately the header files myfunctions1.h and myfunctions2.h have also some global variables defined. These still produce an linker error. Why can't I use the anonymous namespace solution when I use extern "C"? I think this solution woult help with these errors. – JKlein Feb 04 '16 at 08:24
  • The anonymous namespace is a C++ feature and the C linkage isn't compatible with that. The alternative here for the variables is `static` and `extern`, e.g. `extern int MyInt;` and then defined in a single cpp file `int MyInt = 0;`. Experiment with both and see which fits you needs best. – Niall Feb 04 '16 at 08:26
  • Good news, glad it helped. – Niall Feb 04 '16 at 08:47
  • Possible duplicate of [When should I write the keyword 'inline' for a function/method?](http://stackoverflow.com/questions/1759300/when-should-i-write-the-keyword-inline-for-a-function-method) – Niall Feb 04 '16 at 09:03

1 Answers1

0

myfunctions1.h and myfunctions2.h are only header files in which various different functions are declared and defined... but a linker error occurs saying that the functions from myfunctions1.h and myfunctions2.h are defined in model.obj and landscape.obj.

It sounds like the functions declared and defined in the two header files myfunctions1.h and myfunctions2.h have not been declared as inline thus they are found in each object file the header is included in. This is equivalent of including the cpp file into the header file.

When declaring functions in header files, declare them as inline or declare them in an anonymous namespace. Given the extern "C" being used, inline may be preferable in this case.

Niall
  • 30,036
  • 10
  • 99
  • 142