Вопрос:

LNK2005 #include .h file in separate .h causes this

visual-c++

29 просмотра

1 ответ

71 Репутация автора

I am writing in C++ in a C++/CLI project in Visual Studio 2017. I have this third party c library, lets call it lib.h. I have 2 .h files and 2 .cpp files in this project, and am trying to make a DLL. Lets call them A and B.

//A.h
#pragma once
#ifndef INC_A
#define INC_A
extern "C"
{
  #include "lib.h"
}
class A {};
#endif

.

//A.cpp
#include "stdafx.h"
#include "A.h"

.

//B.h
#pragma once
#ifndef INC_B
#define INC_B
extern "C"
{
  #include "lib.h"
}
class B {};
#endif

.

//B.cpp
#include "stdafx.h"
#include "B.h"

Trying to do this throws error LNK2005:

function already defined in A.obj   File: B.obj

I already tried putting an #ifndef around the #include lib.h but it didn't work (I think because each cpp file that is compiled is independent of each other)

Merging the two .cpp files and .h files together might work, but I don't want to do that.

Eventually I would like to refer to B from A, but can't do that until this problem is resolved. I cannot change lib.h at all by the way. Note: the code above is just a small representation of my code. If the code above should compile then I am probably missing something crucial in the example

Автор: adrotter Источник Размещён: 08.11.2017 10:58

Ответы (1)


-1 плюса

71 Репутация автора

Решение

So it was not a function that was already defined, but it was actually a struct. In the .h file, there were some structs that were not static, so I added the static keyword to their declaration and now it works

Автор: adrotter Размещён: 09.11.2017 09:40
Вопросы из категории :
32x32