[英]Linker error when Including own resource files in dll project Microsoft Visual studio 2015
我正在嘗試使用C ++使用Microsoft Visual Studio 2015構建dll文件。 我既不使用/ clr,也不使用預編譯的頭文件。 我必須包括兩個自己的標頭和c ++文件(例如model.h和cpp以及landscape.h和cpp)。 這兩個文件都包含相同的頭文件(例如,functions1.h,functions2.h)。 model.h和landscape.h包含在dll.h中,相應的cpp文件作為資源文件包含在項目中。 當我對此進行編譯時,我收到鏈接器錯誤,因為來自function1.h和function2.h的所包含頭文件中的方法被多次包含了。 但是,如果僅將這些頭文件包含在dll.h文件中,則會出現編譯器錯誤,因為在model.h和landscape.h中不知道這些頭文件中定義的方法。 我如何理解此錯誤是因為每個資源文件都被編譯為一個obj文件,並且鏈接程序在兩個obj文件中都找到了相同的方法,這產生了錯誤。 但是我不知道如何解決這個問題。 我會很樂意為您提供幫助。 干杯喬阿希姆
嗯...也許我沒有明確我的問題。 我有一個dll項目的標頭和cpp文件
//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
}
}
這是對應的.cpp文件
//dllproject.cpp
#include "dllproject.h"
using namespace std;
extern "C"
{
namespace dllproject
{
//export functions
}
}
包含的model.h和landscape.h是自己的項目,其中定義了不同的類,結構和函數,並且包括具有各種功能的其他頭文件。 在model.h中,包括landscape.h,但在landscape.h中不包括model.h
//model.h
#pragma once
#include <myfunctions1.h>
#include <myfunctions2.h>
#include <landscape.h>
//declarations of classes and methods
和相應的cpp文件
//model.cpp
#include <model.h>
//definitions of the declared methods
類似的是landscape.h文件
//landscape.h
#pragma once
#include <myfunctions1.h>
#include <myfunctions2.h>
//declaration of multiple classes and methods
和相應的cpp文件:
//landscape.cpp
#include <landscape.h>
//definitions of the declared methods
myfunctions1.h和myfunctions2.h只是在其中聲明和定義了各種不同功能的頭文件。
MVS2015在外部依賴文件夾中找到landscape.h和model.h,可以將其包含在內。 如果我進行編譯,則找不到cpp文件中的方法定義。 如果我將它們添加為資源文件,則會找到方法,但是會發生鏈接器錯誤,指出myfunctions1.h和myfunctions2.h中的函數在model.obj和landscape.obj中定義。
如果有人可以幫助我,我將非常高興。 干杯喬阿希姆
myfunctions1.h和myfunctions2.h只是頭文件,在其中聲明和定義了各種不同的函數...但是發生鏈接器錯誤,指出myfunctions1.h和myfunctions2.h中的函數在model.obj和landscape.obj中定義。
聽起來好像在兩個頭文件myfunctions1.h和myfunctions2.h中聲明和定義的函數尚未聲明為inline
因此可以在包含該頭文件的每個目標文件中找到它們。這等效於將cpp文件包含在文件頭中。頭文件。
在頭文件中聲明函數時,請將它們聲明為inline
或在匿名名稱空間中聲明。 給定使用extern "C"
符號extern "C"
,在這種情況下,最好使用inline
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.