0
votes

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

1
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
Are these functions declared in different namespaces?Niall
no own namespaces are used (only in the dllproject.h/cpp)JKlein
Are the functions in declared and defined in myfunctions1.h and myfunctions2.h declare with an inline?Niall
@Niall no they are not. Just regulare functions like e.g. double myFunktion(string input_string)JKlein

1 Answers

0
votes

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.