问题
I want to build a simple library with some undefined reference, as you can see in function_two.c, I call three()
which is not defined.
header.h
void one();
void two();
void three();
function_one.c
#include <stdio.h>
#include "header.h"
void one() {
printf("one\n");
}
function_two.c
#include <stdio.h>
#include "header.h"
void two() {
printf("two\n");
three();
}
Using Linux
, I compile both files and then I link them
linux:~/example$ gcc -c -Wall -Werror -fpic function_one.c
linux:~/example$ gcc -c -Wall -Werror -fpic function_two.c
linux:~/example$ gcc -shared -o library.so function_one.o function_two.o
I'm able to create the library, which obviously has three
undefined (U
)
linux:~/example$ nm library.so
0000000000000538 T _init
................
000000000000066a T one
U three
000000000000067d T two
Doing the same thing using MinGW
under Windows10
, I'm not able to build the library due to the undefined reference to function three
windows:/c/example$ gcc -c -Wall -Werror -fpic function_one.c
windows:/c/example$ gcc -c -Wall -Werror -fpic function_two.c
windows:/c/example$ gcc -shared -o library.dll function_one.o function_two.o
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: function_two.o:function_two.c:(.text+0x15): undefined reference to 'trhee'
collect2.exe error: ld returned 1 exit status
Why does Linux
let me build the library, but Windows
doesn't?
来源:https://stackoverflow.com/questions/60380196/create-shared-library-with-undefined-reference-using-gcc-unix-windows-differenc