2

I have a conceptual question about writing a library in plain c. I have some functions that I have to use in different programs in the same folder, so I was thinking about writing a library to host these functions. I have to write the whole code in a folder that will be copied to another computer (where the programs will run). If I create and compile the library in this folder, will be the user able to run the programs without rebuilding the library from source or he might have some unpredictable errors? The user will build the programs that use the library anyway, he won't build the lib itself.

Thanks

Lorenzo

8
  • 1
    If he's on a compatible operating system it should work fine. Commented Oct 15, 2019 at 17:04
  • 1
    Static libraries are linked into the program, you don't need to copy the library to the other computer. Commented Oct 15, 2019 at 17:06
  • @JimRhodes He's not distributing the program, just the library. Commented Oct 15, 2019 at 17:08
  • @Barmar: One needs a compatible C implementation. Sources compiled with an old version of GCC might not work with a library compiled with a new version of GCC or with Apple Clang. Commented Oct 15, 2019 at 17:22
  • 2
    The feature you are looking for is called binary compatibility. You need the object modules in the library to be compatible with the object modules built by the user. If they are using the same C implementation (compiler, headers, shared libraries, target platform, settings for ABIs, and more), they should have binary compatibility. Otherwise, there are a variety of issues that can arise regarding binary compatibility. Commented Oct 15, 2019 at 17:25

1 Answer 1

1

In general, no, it is not portable in the sense that a compiled library can be linked on an arbitrary other system. The compiled library has to be compatible to the target architecture, the OS, the compiler system, to name some.


But you have another choice, concluded from your comment: It seems that you also provide some shell script or makefile to build the programs.

Because a library consists of "just" a set of compiled translation units before some of them get linked into the programs, you can take the set of sources of these translations unit and compile them with the sources of each program, where appropriate.

As an example, let's say you have 2 functions (each in its own source file) you use in different combinations in 3 programs. "prg1" uses func1(), "prg2" uses func2(), and "prg3" uses both.

This can be the commands to build the programs with a (static) library:

gcc -c func1.c -o func1.o gcc -c func2.c -o func2.o ar -r lib.a func1.o func2.o gcc prg1.c lib.a -o prg1 gcc prg2.c lib.a -o prg2 gcc prg3.c lib.a -o prg3 

Instead of the library you compile the programs' sources directly:

gcc prg1.c func1.c -o prg1 gcc prg2.c func2.c -o prg2 gcc prg3.c func1.c func2.c -o prg3 

The results are the same, at least as long as you had linked statically to the library.

But even with a shared (dynamic) library the approach will be the same. Shared libraries "only" save some RAM if several programs using them are run concurrently. If only one program runs at a time, a dynamically linked program might need more RAM and loads slower.

Sign up to request clarification or add additional context in comments.

4 Comments

I was under the impression that the C application binary interface is stable across compilers so that at least on a given OS (e.g. Windows or Linux) programs can load and use dynamic libraries compiled elsewhere. A pretty good indicator for that are the hundreds or thousands of DLLs from a variety of sources compiled with a variety of compilers on your typical Windows system. And it won't matter usually which 64 bit Windows you use as long as the library doesn't do anything version specific. The same holds for Linux. (This is, unfortunately, not a given for C++.)
That's what I meant by "compatible". We don't know the systems the OP and his jury use. They might be quite different like Windows and Linux.
Well, you mention the compiler as a dependency -- that is usually not the case though, C binaries are typically compatible across compilers.
"Typically" does not mean "always", especially if you include libraries in "binaries." ;-) See @EricPostpischil's comment on the question. Executables (commonly called "binaries") built by different compilers are expected to be compatible to the target system, else they are erroneous.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.