Skip to content

Can't produce import libraries for executables on Windows #1623

Closed

Description

@jon-turney

Further to the discussion in PR #1567 consider this example

meson.build

project('shared module linked to symbol in executable', 'c')

dl = meson.get_compiler('c').find_library('dl', required : false)
m = shared_module('module', 'module.c')
e = executable('prog', 'prog.c', dependencies : dl)
test('test', e, args: m)

prog.c

#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif

int
main (int argc, char **argv)
{
#ifdef _WIN32
  HMODULE h = LoadLibraryA(argv[1]);
#else
  void *h = dlopen(argv[1], RTLD_NOW);
#endif

  if (!h) {
    printf ("failed to load library\n");
  }

#ifdef _WIN32
  FreeLibrary(h);
#else
  dlclose(h);
#endif
}

int
func_from_executable(void)
{
  return 42;
}

module.c

extern int func_from_executable(void);

int func(void) {
   return func_from_executable();
}

This fails to build on Windows, because func_from_executable() can't be resolved at link time (as is required by PE/COFF):

[3/4] Linking target cygmodule.dll
FAILED: cygmodule.dll
ccache gcc  -o cygmodule.dll 'module@sha/module.c.o' '-Wl,--as-needed' '-shared' '-Wl,-soname,cygmodule.dll' '-Wl,--out-implib=libmodule.dll.a' '-Wl,-rpath,/wip/exe-implib-example/build/'
module@sha/module.c.o: In function `func':
/wip/exe-implib-example/build/../module.c:7: undefined reference to `func_from_executable'
/wip/exe-implib-example/build/../module.c:7:(.text+0x9): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `func_from_executable'
collect2: error: ld returned 1 exit status
[4/4] Linking target prog.exe
ninja: build stopped: subcommand failed.

However, I can write the following Makefile which builds it successfully. Note the use of libprog.exe.a.

Makefile

all:
        $(CC) $(CFLAGS) -o module.o -c module.c
        $(CC) $(CFLAGS) -o prog.o -c prog.c
        $(CC) $(CFLAGS) -o prog.exe prog.o -Wl,--export-all,--out-implib,libprog.exe.a
        $(CC) $(CFLAGS) -o cygmodule.dll module.o libprog.exe.a -shared -Wl,-soname,cygmodule.dll -Wl,--out-implib=libmodule.dll.a

Activity

  1. nirbheek commented on Apr 18, 2017

    @nirbheek
    Member

    This is interesting, does MSVC also support import libraries for executables or is this a MinGW/Cygwin-only thing?

  2. jon-turney commented on Apr 19, 2017

    @jon-turney
    MemberAuthor

    This is interesting, does MSVC also support import libraries for executables or is this a MinGW/Cygwin-only thing?

    Yes, this can be done with MSVC, also

    NMakefile

    all:
            cl /nologo /c /Fo:module.obj module.c
            cl /nologo /c /Fo:prog.obj prog.c
            cl /nologo /Fe:prog.exe prog.obj /link /implib:prog.lib
            cl /nologo module.obj prog.lib /link /dll /out:module.dll /implib:module.lib
  3. jon-turney commented on Aug 14, 2017

    @jon-turney
    MemberAuthor

    Closed by PR #1955

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    OS:windowsWindows OS specific issuesbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions