Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jon-turney opened this issue Apr 14, 2017 · 3 comments
Closed

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

jon-turney opened this issue Apr 14, 2017 · 3 comments
Labels
bug OS:windows Winodows OS specific issues

Comments

@jon-turney
Copy link
Member

jon-turney commented Apr 14, 2017

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
@nirbheek
Copy link
Member

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

@nirbheek nirbheek added bug OS:windows Winodows OS specific issues labels Apr 18, 2017
@jon-turney
Copy link
Member Author

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

@jon-turney
Copy link
Member Author

Closed by PR #1955

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug OS:windows Winodows OS specific issues
Projects
None yet
Development

No branches or pull requests

2 participants