View Issue Details

IDProjectCategoryView StatusLast Update
0000288file[All Projects] Generalpublic2021-09-20 17:46
ReporterCirn09Assigned Tochristos 
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
Product Version 
Target VersionFixed in Version5.41 
Summary0000288: Naming conflict when compiling libmagic as Windows static library
Descriptionhello,
I want build `libmagic` as static library for Windows. But `libmagic` define the `DllMain` in `magic.c`:
```
/* Placate GCC by offering a sacrificial previous prototype */
BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID);

BOOL WINAPI
DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
    LPVOID lpvReserved __attribute__((__unused__)))
{
    if (fdwReason == DLL_PROCESS_ATTACH)
        _w32_dll_instance = hinstDLL;
    return 1;
}
```
This causes naming conflicts when linking libmagic to a dynamic library:
> `libmagic.lib(magic.obj) : error LNK2005: DllMain already defined in dllmain.obj`

`DllMain` is only used to initialize `_w32_dll_instance`.
In fact, for static libraries, `_w32_dll_instance` is not needed.
`_w32_dll_instance` only used in `get_default_magic`

```
private const char *
get_default_magic(void)
{
...
    /* Fourth, try to get magic file relative to exe location */
        _w32_get_magic_relative_to(&hmagicpath, NULL);

    /* Fifth, try to get magic file relative to dll location */
        _w32_get_magic_relative_to(&hmagicpath, _w32_dll_instance);
    /* Fifth, try to get magic file relative to dll location */
        _w32_get_magic_relative_to(&hmagicpath, _w32_dll_instance);
...
static void
_w32_get_magic_relative_to(char **hmagicpath, HINSTANCE module)
{
...
    if (!GetModuleFileNameA(module, dllpath, MAX_PATH))
...
```
> Parameters
> hModule
> A handle to the loaded module whose path is being requested. If this parameter is **NULL**,
> GetModuleFileName retrieves the path of the executable file of the current process.
> https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulefilenamea

I think to solve this problem need to add a new macro judgment, like:
```
#ifndef BUILD_AS_WINDOWS_STATIC_LIBARAY
BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID)
...
#endif
```
Tagsbuild

Activities

christos

2021-09-20 17:46

manager   ~0003646

Added as suggested, thanks!

Issue History

Date Modified Username Field Change
2021-09-13 07:18 Cirn09 New Issue
2021-09-13 07:18 Cirn09 Tag Attached: build
2021-09-20 17:45 christos Assigned To => christos
2021-09-20 17:45 christos Status new => assigned
2021-09-20 17:46 christos Status assigned => resolved
2021-09-20 17:46 christos Resolution open => fixed
2021-09-20 17:46 christos Fixed in Version => 5.41
2021-09-20 17:46 christos Note Added: 0003646