Building dll library which uses other static libraries

I’m trying to build dll in C (using Cygwin).

It is my umfsolver.c. It includes library UMFPACK, which includes libraries AMD, SuiteSparse_config, CHOLMOD and others.

umfsolver.c contains functions:

__declspec(dllexport) int __stdcall func1()
{
    return 123;
}
__declspec(dllexport) int __stdcall func2()
{
    int* a;
    a = (int*) malloc(sizeof(int)*10);
    return 223;
}
__declspec(dllexport) void __stdcall func3()
{
    printf("hellon");
}
__declspec(dllexport) int __stdcall func4()
{
    int* a;
    a = (int*) malloc(sizeof(int)*10);
    if (a == NULL)
    {
        return 111;
    }
    return 133;
}

When I build dll:

cc    -O3 -fexceptions -fPIC -DNBLAS  -I../Include -I../../AMD/Include -I../../SuiteSparse_config -c -DBUILD_DLL umfsolver.c
cc1: warning: fPIC ignored for target (all code is position independent)
cc    -O3 -fexceptions -fPIC -DNBLAS  -I../Include -I../../AMD/Include -I../../SuiteSparse_config -shared -o umfsolver.dll umfsolver.o ../Lib/libumfpack.a ../../AMD/Lib/libamd.a ../../SuiteSparse_config/libsuitesparseconfig.a ../../CHOLMOD/Lib/libcholmod.a ../../COLAMD/Lib/libcolamd.a ../../metis-4.0/libmetis.a ../../CCOLAMD/Lib/libccolamd.a ../../CAMD/Lib/libcamd.a     -lm -lrt ../../CHOLMOD/Lib/libcholmod.a ../../COLAMD/Lib/libcolamd.a ../../metis-4.0/libmetis.a ../../CCOLAMD/Lib/libccolamd.a ../../CAMD/Lib/libcamd.a    -Wl,--add-stdcall-alias

it builds with no errors. But there is a strange behaviour of this dll.

When I call func1() and func2() from another program (using static dllimport), I get result 123 and 223, so it works fine.

When I call func3() or func4(), my program stops respond, like if it goes to infinite loop.

When I build just a .exe file, everything works fine:

cc    -O3 -fexceptions -fPIC -DNBLAS   -I../Include -I../../AMD/Include -I../../SuiteSparse_config -o umfsolver umfsolver.c ../Lib/libumfpack.a ../../AMD/Lib/libamd.a ../../SuiteSparse_config/libsuitesparseconfig.a ../../CHOLMOD/Lib/libcholmod.a ../../COLAMD/Lib/libcolamd.a ../../metis-4.0/libmetis.a ../../CCOLAMD/Lib/libccolamd.a ../../CAMD/Lib/libcamd.a     -lm -lrt ../../CHOLMOD/Lib/libcholmod.a ../../COLAMD/Lib/libcolamd.a ../../metis-4.0/libmetis.a ../../CCOLAMD/Lib/libccolamd.a ../../CAMD/Lib/libcamd.a
cc1: warning: fPIC ignored for target (all code is position independent)

So, how can I solve this strange problem?

Thanks everyone!


Source: gcc

Leave a Reply