4

First off, I'm relatively new to C - I know the language somewhat, but never looked too much into the whole build process.

From what I'm seeing, if I want to use a third party library and have it in source code form, I would compile that library separately (which yields a platform and architecture specific binary blob?), copy over the .h files from that library into my own project, and then tell my linker that it should link against that.

From what I've read, it seems that I should be using the same compiler and make sure they compile for the same platform, which makes me wonder how I should structure my project and build.

It seems unwise to work with pre-compiled libraries, and instead I should check in the source code of those libraries (e.g., SDL, Lua, NCurses, any JSON Parser etc.) and then build those libraries from scratch every time I (re)build my application.

That seems like a huge pain though, because of different build systems. I come from the C# world where packaging systems (and the mostly platform-independent binary library format) make this a lot easier.

What is the right way to work with libraries in a C project? (No C++)

Michael Stum
  • 1,778
  • 2
  • 17
  • 22

1 Answers1

5

You're basically right so far. The magic is that the C world has packaging systems too, and they come with your Operating System!

Now, before you get too excited, this depends on your platform. Modern Linux distributions have package managers and the more popular libraries are distributed in binary form by these systems. For example, a program that needs the MySQL connector probably depends on the mysql package. These systems also often deploy source for those libraries for development use (for precisely your use case, in fact), for example the mysql-devel package.

For less common libraries, you may need to download the source yourself. You could check it into your own version control system, but this is considered an anti-pattern: it would be better to instead just document in your README that visibility of third-party source is required on any hypothetical build system. A developer working on your project would extract it to the appropriate place, e.g. /usr/local/include and /usr/local/lib as needed.

And on Windows that's pretty much a requirement anyway.

I have no idea about OSX or more exotic platforms.

For what it's worth, this is all the same for C++ and, indeed, for basically all other "mainstream" back-end programming languages that I can think of right now.

Lightness Races in Orbit
  • 8,755
  • 3
  • 41
  • 45