2014-07-01 141 views
1

我想創建一個解決方案,其中一個項目是.exe和另一個項目是一個簡單的dll。我想要學習的是如何鏈接兩個項目。我已搜查堆棧溢出,發現我所遵循非常好的答案,比如宣佈在右側頭部浴:LNK2019解決方案與dll項目

Properties - >Configuration Properties - >C/C++ - >General - >Additional Include Directories

然後設置的.lib上:

Properties - >Configuration Properties - >Linker - >Input - >Additional Dependencies

我用宏來生成.lib文件。這裏是我的簡化代碼:

.exe: CPP:

#include "stdafx.h" 
#include "../ConsoleApplication2/HelloWorld.h" 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    hello_world hw; 
    hw.printHello(); 
    getchar(); 
    return 0; 
} 

的DLL: 頭:

#pragma once 
#ifdef is_hello_world_dll 
#define hello_world_exp __declspec(dllexport) 
#else 
#define hello_world_exp __declspec(dllimport) 
#endif 


class hello_world_exp hello_world 
{ 
public: 
    hello_world(); 
    ~hello_world(); 
    void printHello(); 
}; 

CPP:

#include "stdafx.h" 
#include "HelloWorld.h" 
#include <iostream> 

hello_world::hello_world() 
{ 
} 

hello_world::~hello_world() 
{ 
} 

void printHello() 
{ 
    std::cout << "Hello World" << std::endl; 
} 

的注意事項:解決方案編譯罰款時,我不打電話然而當我做調用它,鏈接器將生成:

錯誤1個錯誤LNK2019:無法解析的外部符號 「__declspec(dllimport的)市民:無效__thiscall程序hello_world :: printHello(無效)」?(__imp_ printHello @程序hello_world @ @QAEXXZ)函數_wmainç引用:\用戶\ usteinfeld \桌面\私人\學生\雅娜\ ConsoleApplication1 \ ConsoleApplication1 \ ConsoleApplication1.obj ConsoleApplication1

回答

3

該功能是基於你如何寫它定義爲一個自由函數

void printHello() 

它屬於類hello_world所以你應該範圍它是這樣

void hello_world::printHello() 
{ 
    std::cout << "Hello World" << std::endl; 
} 
+0

我有一種感覺,它是這樣的。一旦時限到了,我會盡快回復!謝謝! – Steinfeld