简体   繁体   中英

Link .a library to .o object so only .o needs to be included when building

I'm using a pre-built library called 'libdscud-6.02.a', which includes a lot of low level I/O calls for some specific hardware. From this, I created some wrapper functions which I compiled into an object file called 'io.o'.

Now, I have a few programs which I'm compiling with these I/O functions and instead of having to do this:

gcc libdscud-6.02a io.o -o test test.c

I would like to just have this:

gcc io.o -o test test.c

Is there any way to link the .a file into the .o file so I only need to include the .o file when compiling binaries?

您可以执行相反的操作,并使用ar将io.o文件添加到.a文件:

ar q libdscud-6.02.a io.o

One solution would be simply to use a make variable:

IO_STUFF = libdscud-6.02a io.o

...

$(CC) $(IO_STUFF) ...

AFAIK it is not possible to link .a library and .o file to create another intermediate file ie file which is not linked like .o file. The solution provided by Burton Samograd look like a very good option; but in case you are not allowed to modify .a library file then you can follow suggestion provided by DarkDust in case you are building using make .
However you can create a shared library .so file, from a .a library file and .o file (I think that is what Michael Burr is trying to convey). You can use only the shared library instead of both .a & .o file to generate your executable as follows:
Generate shared library gcc io.o libdscud-6.02.a -shared -o io.so (Please note that the order of files passed for linking is important)
Build your source with gcc io.so -o test test.c . To execute your executable path of io.so should be in the look up path of loader (ld) ie LD_LIBRARY_PATH.
The right way to work with shared object would be to create a libio.so which is the naming convention and not io.so and build code as gcc test.c -o test -L<path_to_libio.so> -lio and path to libio.so should be in ld's look up path for executing the output executable.
I know creating shared library just to avoid addition of another file for compilation does not seem to be what you want to do ...but it is just for your info in case you didn't already know :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM