Hello,
I am creating a DLL that I want to build as 32-bit version and as 64-bit version. I am using the TDM gcc compiler, but I also want programmers that use visual studio to be able to use it, so after compiling I generate a corresponding .lib file as an alternative to the .a file.
This works very well for the 32 bit version, but it doesn't work for the 64 bit version. To be specific, the linking of the program that should use the DLL fails with the error: "error adding symbols: File format not recognized".
So I dumbed the problem down as far as I could to ensure it is not something else i did wrong, but can't find a cause for this problem.
This is the dumbed down source for the DLL.
DLL header file (main.h):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#ifndef __MAIN_H__
#define __MAIN_H__
#include <windows.h>
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C"
{
#endif
void DLL_EXPORT __cdecl SomeFunction(const LPCSTR sometext);
#ifdef __cplusplus
}
#endif
#endif // __MAIN_H__
|
DLL implementation file (main.cpp)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include "main.h"
// a sample exported function
void DLL_EXPORT SomeFunction(const LPCSTR sometext)
{
MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
}
extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break;
case DLL_PROCESS_DETACH:
// detach from process
break;
case DLL_THREAD_ATTACH:
// attach to thread
break;
case DLL_THREAD_DETACH:
// detach from thread
break;
}
return TRUE; // succesful
}
|
I use TDM-GCC-32 version 5.1.0 to build the 32 bit version of the DLL. This gives me libtest_dll_x32.a, libtest_dll_x32.def and test_dll_x32.dll.
Subsequently I use Visual C++ 2015 build tools from the command line to generate a .lib file based on the .def file. Like this:
1 2 3 4
|
c:
call "C:\Program Files (x86)\Microsoft Visual C++ Build Tools\vcbuildtools.bat" x86
cd C:\temp\test_dll\bin
lib /def:libtest_dll_x32.def /MACHINE:X86 /out:libtest_dll_x32.lib
|
This gives me libtest_dll_x32.exp and libtest_dll_x32.lib.
For the 64 bit version I do the same:
I use TDM-GCC-64 version 5.1.0 to build the 64 bit version of the DLL. This gives me libtest_dll_x64.a, libtest_dll_x64.def and test_dll_x64.dll.
Subsequently I use Visual C++ 2015 build tools from the command line to generate a .lib file based on the .def file. Like this:
1 2 3 4
|
c:
call "C:\Program Files (x86)\Microsoft Visual C++ Build Tools\vcbuildtools.bat" x86_amd64 // I have also used the native x64 option, but with the same result.
cd C:\temp\test_dll\bin
lib /def:libtest_dll_x64.def /MACHINE:X64 /out:libtest_dll_x64.lib
|
This gives me libtest_dll_x64.exp and libtest_dll_x64.lib.
This is the dumbed down project that I use to use the DLL.
1 2 3 4 5 6 7 8 9
|
#include <iostream>
#include "../test_dll/main.h"
int main()
{
SomeFunction("Hello world!");
return 0;
}
|
For the 32 bit target I again use TDM-GCC-32 and I link to libtest_dll_x32.lib. This builds, links and executes as expected.
For the 64 bit target I again use TDM-GCC-64 and I link to libtest_dll_x64.lib. This builds the object file, but does not link the executable. The linking process fails with the error"error adding symbols: File format not recognized".
If I link the 64 bit target to libtest_dll_x64.a it does properly link and the executable works as expected.
I would really like to know how I can get a valid .lib file for my 64-bit dll and why my current approach is possible for 32-bit applications, but not for 64-bit applications.
Kind regards, Nico