0
我已經在Windows 7 64位上用C++編寫了一個非常簡單的DLL程序。我做了兩個版本,64位和32位。我使用-m32
標誌設置鏈接器和編譯器編譯的32位項目。我使用-std=c++11
。下面是它的代碼:使用CodeBlocks編譯64位DLL會導致鏈接器錯誤
main.h
#ifndef __MAIN_H__
#define __MAIN_H__
#include <windows.h>
/* To use this exported function of dll, include this header
* in your project.
*/
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C"
{
#endif
void DLL_EXPORT SomeFunction(const LPCSTR sometext);
#ifdef __cplusplus
}
#endif
#endif // __MAIN_H__
的main.cpp
#include "main.h"
// a sample exported function
void DLL_EXPORT SomeFunction(const LPCSTR sometext)
{
MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
MessageBoxA(0, "DLL_PROCESS_ATTACH", "DLL Message", MB_OK | MB_ICONINFORMATION);
break;
case DLL_PROCESS_DETACH:
MessageBoxA(0, "DLL_PROCESS_DETACH", "DLL Message", MB_OK | MB_ICONINFORMATION);
break;
case DLL_THREAD_ATTACH:
MessageBoxA(0, "DLL_THREAD_ATTACH", "DLL Message", MB_OK | MB_ICONINFORMATION);
break;
case DLL_THREAD_DETACH:
MessageBoxA(0, "DLL_THREAD_DETACH", "DLL Message", MB_OK | MB_ICONINFORMATION);
break;
}
return TRUE; // succesful
}
的DLL編譯罰款,並在32位版本的效果很好。但是,當我通過刪除所有內容的-m32
選項編譯爲64位時,出現鏈接器錯誤:
obj \ Release \ main.o:main.cpp ||對`_imp_MessageBoxA'|的未定義引用
用-m64
替代並沒有幫助。這裏是我的MingW版本的詳細信息:
Using built-in specs.
COLLECT_GCC=x86_64-w64-mingw32-g++.exe
COLLECT_LTO_WRAPPER=E:/Program\ Files\ (x86)/CodeBlocks/MinGW/bin/../libexec/gcc/x86_64-w64-mingw32/5.1.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-5.1.0/configure --build=x86_64-w64-mingw32 --enable-targets=all --enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable-libgomp --enable-lto --enable-graphite --enable-cxx-flags=-DWINPTHREAD_STATIC --disable-build-with-cxx --disable-build-poststage1-with-cxx --enable-libstdcxx-debug --enable-threads=posix --enable-version-specific-runtime-libs --enable-fully-dynamic-string --enable-libstdcxx-threads --enable-libstdcxx-time --with-gnu-ld --disable-werror --disable-nls --disable-win32-registry --prefix=/mingw64tdm --with-local-prefix=/mingw64tdm --with-pkgversion=tdm64-1 --with-bugurl=http://tdm-gcc.tdragon.net/bugs
Thread model: posix
gcc version 5.1.0 (tdm64-1)
我會做什麼錯?難道我需要包含一個特殊的64位Windows頭文件嗎?非常感謝任何方向! :)
我知道,這只是爲了測試,但不要從'DllMain'調用'MessageBox'(參見[Dynamic-Link Library Best Practices](https://msdn.microsoft.com/en-us/library/)窗口/桌面/ dn633971.aspx))。不過,調用[OutputDebugString](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363362.aspx)是安全的。順便說一句,因爲你使用的是Code :: Blocks,並且它的默認設置選擇很差:你應該絕對肯定地調用Windows API的Unicode版本(即'MessageBoxW'而不是'MessageBoxA')。 – IInspectable
非常好,謝謝! – bombax