(This gcc 3.3.1(long story is NIST's charge) on Cygwin.)
I compiled some source files with gcc -c -fPIC ...to get .o files.
Then I did:
$ gcc -shared -o foo.dll foo.o bar.o
But when I go to use it:
$ gcc -o usefoo.exe usefoo.o -L. -lfoo
usefoo.o:usefoo.cpp:(.text+0x2e0): undefined reference to `_get_template_size'
collect2: ld returned 1 exit status
However, if using the same .o files , I instead:
$ ar rcs libfoo-static.a foo.o bar.o
Link to this successfully:
$ gcc -o foo.exe foo.o -L. -lfoo-static
What is strange to me is that, as you can see below, this link is present in both .a and .dll. So why the error connecting to .dll?
The link is in the shared library:
$ nm foo.dll|grep get_template
1001b262 T _get_template_size
And this is also in the static library:
$ nm libfoo-static.a |grep get_template
00000352 T _get_template_size
And here is a link to the character generated in the file that wants to use the function:
$ nm usefoo.o
00000000 b .bss
00000000 d .data
00000000 t .text
0000012c T __Z12ErrorMessagei
U ___main
U __alloca
U _atoi
U _get_template_size
0000026c T _main
U _printf
Updated for Marco's answer
/, , ( ):
func1.h:
#ifndef FUNC1_H
#define FUNC1_H
int func1(int i);
#endif
func1.c:
#include "func1.h"
int func1(int i) {
return 2*i;
}
usefunc.c:
#include <stdio.h>
#include "func1.h"
int main() {
printf("%d\n", func1(10));
}
:
$ rm *.o *.dll *.a
$ gcc -fPIC -I. -c func1.c usefunc.c
$ gcc -shared -o func.dll func1.o
$ gcc -L. -o usefunc.exe usefunc.o -lfunc
$ ./usefunc.exe
20