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

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.

  • Read up some more, and it seems that Shared Libraries are the solution that's used in the wild, specifically so that I can use the OS package manager. – Michael Stum Feb 29 '16 at 21:26
  • 1
    For completeness, OS X has and uses /usr/local as Linux does. – jscs Feb 29 '16 at 21:41
  • Most of the mainstream languages have a package manager. Python has pipy, rust has cargo, nodejs has npm, Java has maven, C# has nuget, etc etc. C and C++ are some of the few with a "manual" library management. – Florian Margaine Feb 29 '16 at 21:45
  • @FlorianMargaine: I don't think this is any more "manual" than those. – Lightness Races with Monica Feb 29 '16 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.