If you do not -L
the path to the library you may
specify the library by an absolute or relative pathname. In the
latter case, whatever the library is called, it will be taken
as a linker input unless it has an extension that makes it
appear to be a source file. Thus
gcc -o prog main.c foo.xyz
will compile main.c
and link main.o
with the library ./foo.xyz
if in
fact it is one, whether static or shared, that can satisfy the linkage.
Later
(Before editing, the question did not mention the -x
option).
The introduction of the -x
option as in:
gcc -o prog -x c++ main.cpp foo.xyz
provokes an error like:
foo.xyz:2:1: error: stray ‘`’ in program
/ 0 0 0 0 16 `
^
foo.xyz:3:1: warning: null character(s) ignored
T_Z3foov foo.o/ 0 0 0 644 1544 `
^
foo.xyz:3:4: error: stray ‘\1’ in program
T_Z3foov foo.o/ 0 0 0 644 1544 `
^
...
...
even when foo.xyz
is in fact a library that can satisfy the linkage,
going by an unorthodox name.
As it must, because -x c++
directs gcc to construe subsequent input files
as C++ source until further notice. As documented
-x language
Specify explicitly the language for the following input files (rather than
letting the compiler choose a default based on the file name suffix).
This option applies to all following input files until the next -x option.
And further notice never comes.
Of course, this does not happen in the case, e.g. of:
gcc -o prog -x c++ main.cpp -L. -l:foo.xyz
because -l:foo.xyz
isn't an input file but a linker option (which, in
conjunction with -L.
, specifies an input file).
To avoid this outcome you must drop the insistence that foo.xyz
is
a C++ source file by cancelling -x c++
before it is reached, like:
gcc -o prog -x c++ main.cpp -x none foo.xyz
As documented:
-x none
Turn off any specification of a language, so that subsequent files are handled
according to their file name suffixes (as they are if -x has not been used at all).