Functions not returning the correct value in DLL

This is so frustrating! I don't know why this is happening. I have a file named weirdDLL.c :

double five() {
    return 5.0;
}

      

I have another file called weirdTest.c

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv) {
    double f = five();
    if (f != 5.0) {
        printf("Test failed with %f", f);
        return 1;
    }
    return 0;
}

      

I expect that when compiling and linking to the DLL, the code in the weirdTest will exit without errors. I am compiling 64 bit Windows 7 using gcc (cygwin) using the commands:

gcc -c weirdDLL.c
gcc -shared -o weirdDLL.dll weirdDLL.o
gcc -o test weirdtest.c -L./ -l weirdDLL
./test

      

Output:

Test failed with 0.000000

      

The DLL seems to link correctly because the compiler doesn't complain about the missing "five" feature. Also, when I add print statements to the DLL, they show up in order. What have I done wrong?

+3


source to share


2 answers


Just a wild guess:



You don't seem to be declaring a prototype for 5 () in weirdTest.c, so the compiler treats the return type of this function as int. The resulting conversion from int to double will mess up your original double value.

+5


source


use dumpbin (windwos) or nm (linux) to list your DLL export function



and see if 5 () is exported or not

0


source







All Articles