Reputation:
I am attempting to link an assembly main file to external functions, but I keep getting the 'undefined reference to function X in main'.
I have a folder full of external files which all are correct names and are .s files.
I do know that others have this issue as well, but they seems mostly focused on C and assembly files but this is only linking assembly files.
This is my code when compiling:
gcc -o main.o main.s -I lib/ -no-pie
Error that occurs when trying to compile:
/tmp/ccMgNx1L.o: In function `main':
(.text+0xa): undefined reference to `funcA'
(.text+0xf): undefined reference to `funcB'
(.text+0x14): undefined reference to `funcC'
Some extra clarity: The lib folder contains all the functions referenced in the main.s file. I wish to compile all files into an executable.
Upvotes: 1
Views: 2225
Reputation: 364583
I think you want gcc -no-pie -o main main.s lib/*.s
to assemble all your source files and link them all into one executable.
Note the lack of .o
on the output file, because you're assembling+linking to an executable, not just to an unlinked .o
object file.
-I
sets the include path for #include <>
and #include ""
. It's totally irrelevant here. So would -L lib/
because you didn't tell gcc to link any libraries. Only where to look for any libraries if you did link any. (But you don't have .a
or .so
libraries, you have asm source files that aren't assembled yet.)
If you had main.S
(capital S), gcc would run it through the C preprocessor first, so you could #include "foo.s"
in main for all the functions you actually use. And -I
to set the include path would avoid needing #include "lib/foo.s"
Upvotes: 3