使用 CodeBlocks 编译 64 位 DLL 会出现链接器错误
Compiling a 64-bit DLL with CodeBlocks gives linker errors
我已经在 Windows 7 64 位上用 C++ 编写了一个非常简单的 DLL 程序。我制作了两个版本,64 位和 32 位。我使用设置了 -m32
标志的链接器和编译器编译的 32 位项目。我使用 -std=c++11
。这是它的代码:
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 header?非常感谢任何方向! :)
修好了,我是个白痴。很久以前,我在 "Compiler and debugger." 下的链接器设置中将 -m32
设置为永久标志,将其删除,现在可以正常工作了。
我已经在 Windows 7 64 位上用 C++ 编写了一个非常简单的 DLL 程序。我制作了两个版本,64 位和 32 位。我使用设置了 -m32
标志的链接器和编译器编译的 32 位项目。我使用 -std=c++11
。这是它的代码:
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 header?非常感谢任何方向! :)
修好了,我是个白痴。很久以前,我在 "Compiler and debugger." 下的链接器设置中将 -m32
设置为永久标志,将其删除,现在可以正常工作了。