This is so frustrating! I don’t know why this is happening. I have a file called 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 compiled and linked with the DLL, the code in weirdTest will exit without error. I am compiling on 64bit Windows 7 using gcc (cygwin) with the commands:
gcc -c weirdDLL.c
gcc -shared -o weirdDLL.dll weirdDLL.o
gcc -o test weirdtest.c -L./ -l weirdDLL
./test
The output is:
Test failed with 0.000000
It seems like the DLL is getting linked up correctly because the compiler does not complain about missing function “five.” Additionally, when I put print statements in the DLL code, they show up fine. What did I do wrong?
Just a wild guess:
You don’t seem to declare a prototype for five() in weirdTest.c, this is why the compiler treats the return type of that function as int. The resulting conversion from int to double messes up your original double value.