1

I have a source file which is named foo.bar and for the purpose of this question cannot be renamed or linked to. Say it's a C++ source file. Now, I want to compile it and link it, in the same command, using a library at path/to/weird_lib_file. Now, if the source filename was foo.cpp, I could do:

gcc -o foo foo.cpp /path/to/weird_lib_file

and that would work. But if I write

gcc -o foo -x c++ foo.cpp /path/to/weird_lib_file

it doesn't work. Now, I could do

gcc -o foo -x c++ foo.cpp -L /path/to/ -l:weird_lib_file

as suggested here:

How to link using GCC without -l nor hardcoding path for a library that does not follow the libNAME.so naming convention?

but for some reason which I will also not go into, I would rather not -L that folder. Can I still force GCC to link against that individual library file somehow? It seems that -l:/path/to/weird_lib_file doesn't work.

Community
  • 1
  • 1
einpoklum
  • 118,144
  • 57
  • 340
  • 684

1 Answers1

1

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).

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
  • That doesn't work for me... hmmm... maybe I dropped some switches from the example above. Let me edit that. – einpoklum Dec 04 '16 at 20:55
  • Awesome, thanks. I didn't realize I could -x twice. So, in case you're interested, [here](http://stackoverflow.com/a/40665580/1593077) is the motivation (although actually the library name does end with `.so`). – einpoklum Dec 04 '16 at 22:57