[問題] code::blocks下編譯dll遇到問題
開發平台(Platform): (Ex: Win10, Linux, ...)
Win10
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
GUN GCC Compiler
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
沒有
問題(Question):
在製作DLL的時候,如果我把function放在main.cpp時(如下面程式碼中的AddInt())
可以正常編譯,但是若放在其他檔案時(如下面程式碼中的AddExt())會編譯失敗。
我想應該是編譯環境路徑設定的問題,可是不清楚要改哪個部分,想問問看有沒有人
有類似經驗。
餵入的資料(Input):
預期的正確結果(Expected Output):
錯誤結果(Wrong Output):
程式碼(Code):(請善用置底文網頁, 記得排版)
.\add.h // 我寫的function header.
int AddExt(const int, const int);
.\add.c // 我寫的function.
#include "add.h"
int AddExt(const int a, const int b) {
return (a+b);
}
.\main.h // Code::Blocks DLL 專案自動產生,我修改部分內容。
// 部分註解跟前置處理器忽略的部分我沒貼上。
#ifndef __MAIN_H__
#define __MAIN_H__
#include "add.h" // 我增加的部分
#include <windows.h>
#define DLL_EXPORT __declspec(dllexport)
int DLL_EXPORT SomeFunction(const int, const int);
#endif
.\main.cpp // Code::Blocks DLL 專案自動產生,我修改部分內容。
// 部分註解跟前置處理器忽略的部分我沒貼上。
#include "main.h"
static int AddInt(const int a, const int b) {
return (a+b);
}
extern int AddExt(const int, const int);
int DLL_EXPORT SomeFunction(const int a, const int b)
{
//return (AddInt(a,b)); // OK
//return (a+b); // OK
return (AddExt(a,b)); // 編譯不過,錯誤訊息如下。
}
extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpvReserved) {
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
錯誤訊息:
obj\Release\main.o:main.cpp|| undefined reference to `AddExt(int, int)'
補充說明(Supplement):
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.132.98.137
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1493210103.A.788.html
推
04/26 21:15, , 1F
04/26 21:15, 1F
推
04/26 21:18, , 2F
04/26 21:18, 2F
只要把#include "add.h"換個地方,問題就非常神奇的解決了。
.\main.h // Code::Blocks DLL 專案自動產生,我修改部分內容。
// 部分註解跟前置處理器忽略的部分我沒貼上。
#ifndef __MAIN_H__
#define __MAIN_H__
#include <windows.h>
#define DLL_EXPORT __declspec(dllexport)
#include "add.h" // 改放在這裡就能編譯過了。
int DLL_EXPORT SomeFunction(const int, const int);
#endif
放在這兩個地方感覺上應該沒有什麼不同,畢竟兩者之間只差了一個#define語句,
我也搞不太懂為什麼,留給高手解答吧。
VS商業使用要錢的Orz
※ 編輯: icetofux (220.132.98.137), 04/26/2017 21:28:10
→
04/26 23:49, , 3F
04/26 23:49, 3F
推
04/26 23:58, , 4F
04/26 23:58, 4F
→
04/26 23:59, , 5F
04/26 23:59, 5F
→
04/27 00:02, , 6F
04/27 00:02, 6F
→
04/27 01:06, , 7F
04/27 01:06, 7F
→
04/27 01:08, , 8F
04/27 01:08, 8F
→
04/27 05:43, , 9F
04/27 05:43, 9F
→
04/27 09:22, , 10F
04/27 09:22, 10F
→
04/27 09:23, , 11F
04/27 09:23, 11F
推
04/27 19:20, , 12F
04/27 19:20, 12F
→
04/27 20:55, , 13F
04/27 20:55, 13F
→
04/27 20:55, , 14F
04/27 20:55, 14F
→
04/27 20:55, , 15F
04/27 20:55, 15F
→
04/27 20:56, , 16F
04/27 20:56, 16F
→
04/27 20:57, , 17F
04/27 20:57, 17F