link erlang nif on windows - c

I try to compile a erlang nif plugin on windows using the cygwin gcc.
It compiles fine but the linker issues some errors:
undefined reference to `_enif_get_int'
I'm currently linking against ei.lib and erl_interface.lib.
None of these contain the required symbols. Did I miss something?

It looks rather like you have a windows module (which expects symbols starting with underscore) linking against cygwin libraries (which typically don't export symbols with underscore).

Related

Eliminate dependency on MinGW-specific DLLs when compiling dynamic library

I am using msys2 to compile a library that uses autotools as the build system. The end result is a DLL. This DLL ends up referring to the following other DLLs that come with msys2:
libgcc_s_seh-1.dll
libstdc++-6.dll
libwinpthread-1.dll
How can I link these statically and eliminate these dependencies?
There are other questions dealing with this (example) and the solutions suggest using the options -static-libgcc -static-libstdc++. These work when linking an .exe, but they do not seem to work when linking a .dll.
I set the following variables before running ./configure (and checked the output to verify that these compiler options are really being used), but Dependency Walker still shows a dependency on libstdc++-6.dll, just as before.
export CFLAGS="-static-libgcc -static-libstdc++" CXXFLAGS="-static-libgcc -static-libstdc++" LDFLAGS="-static-libgcc -static-libstdc++"
(I assume these must only go in LDFLAGS, but since I don't have a full understanding, I also added them in CFLAGS and CXXFLAGS.)
Is there a way to get rid of these dependencies when linking a DLL, not an EXE?
The library is written in a mix of C and C++ and has a C API.
Try just using the -static option in LDFLAGs. I tested it just now in MSYS2 and it worked for me.

MSVC project missing "__popcountdi2" when linking against GCC-built static library

I have a windows executable project that links against a static library (.lib) built with GCC 6 (MinGW). The following error occurs during compilation:
LNK2019 unresolved external symbol __popcountdi2 referenced in function ...
The symbol is linked in as a result of using a GCC built-in function __builtin_popcount(), which resides in libgcc. However, despite adding -static-libgcc as an argument to gcc, the problem still persists.
Is there a way for my library (.lib) to contain parts of libgcc rather than requiring libgcc to be on the system for an executable to link against? Is some way around having to ship the library with libgcc?
__builtin_popcount() is not the only built-in function that I'm currently using. The library makes use of __builtin_bswap32(), which doesn't seem to run into this issue.
I'm using GCC 6.1.0. Updated to 6.2.0; same issue.
The issue was that on windows, MinGW (GCC) was not detecting the current CPU architecture properly when -march=native was passed in. It was falling back to an architecture that did not support POPCNT as a native instruction (probably i686). As a test, -mpopcnt was added to the build and everything worked fine.
The fix is to manually specify the architecture using -march=.
Furthermore: is this a bug with MingGW since the native architecture is not polled for properly?

Make AIX load all shared symbols at run time?

I'm on AIX 5.3, working with C.
I have an application (foo) that links in a shared library (lib1.so) at run time, then dynamically loads another library (lib2.so) via dlopen(). lib2.so uses some functions in lib1.so that foo does not use. When I execute the application, I get an error similar to:
rtld: 0712-001 Symbol someLibFunc was referenced from module
/libdir/lib2.so(), but a runtime definition of the symbol was not found.
I don't believe that changing the dlopen() flags would have any effect, since my issue seems to have something to do with what symbols are imported when run-time linking occurs. Is there some type of ld option I can use when building foo to force it to import all shared library symbols? This same build works great in my Linux environment.
I found the culprit.
I ran 'dump -Tv' on lib1.so and found that the function I expected to be exported was not there (although it did show up in nm, oddly enough). The library was linked with -bexpall so all symbols should be there. I dug deeper into the ld man page and saw that expall did not export symbols prefixed with an underscore (_). The function I was trying to use began with an underscore. I found the 'expfull' ld option, which exports symbols prefixed with an underscore, rebuilt lib1.so with that option, and everything is good now.

When I try to run MPI code, I get undefined reference to MPI_File_Seek#12, What might be the reason?

I've been trying to run the code from https://github.com/dungtn/mpi-floyd/blob/master/floyd2d.c in my system. I'm using CodeBlocks IDE and MS-Mpi. When I try to compile the code, it says undefined reference to MPI_file_seek#12. Does this mean MS mpi does not support this function or why does this happen?
This usually happens if you are trying to link 32-bit code with 64-bit libraries. The fact that the unresolved symbol has #12 in its name means that the compiler is expecting that MPI_File_seek is an stdcall function. stdcall is mainly used for DLL functions and only on x86 (x64 uses a different calling convention similar to fastcall). If you are linking against the 64-bit import library of MS-MPI, the decorated symbol won't be found in the library and such an error will occur.
Double check what version of MS-MPI you have and also your project settings and make sure that both have the same "bitness".
Change the project settings in Code::Blocks to a C project (rather than C++ project, what you have currently). It may be easier to create a brand new C project and import the file there. Double check that Code::Blocks in running gcc and not g++ to compile your code (floyd2d.c).
If it still doesn't work, please post the full compiler and linker output of Code::Blocks, including the commands run and their output messages.

MinGW libraries issues

In the process of porting a C project from Linux to Windows
Have installed MinGW
Have compiled my shared library using a Makefile
This produces libExample.so
Now I'm trying to link this shared library to a test harness so I can see if everything is working as expected
In the harness Makefile I specify the location of the library, e.g. -LE:/libExample_dir and the name of the library -lExample
but its complaining it cannot find the library, i.e. linker is failing with cannot find -lExample - is there some difference with windows regarding .so and .dll or perhaps pathnames that I am missing?
You need to fix the make file so shared libraries are generated with a .dll extension.
If I had to guess, I'd say that while renaming the generated file is enough to make the linker happy, the loader still expects the .so extension because that's the name that was compiled in...
Using MinGw to compile C code to produce a shared library, remember to rename the output from libExample.so to libExample.dll otherwise the linker will fail to find your library

Resources