0

I've seen plenty of online tutorials explaining how to use GLFW and libcurl, but where do I actually place the files I downloaded? For example I downloaded this file for GLFW - And these are the contents The C+ file wasn't in there I put that there :p So how would I add the library to any of my .c files? I've looked everywhere, I might just not be using the right keywords. And second, how can I have multiple libraries at the same time? And lastly, what do I put in the <> in the include?

I'm using windows 10 and am using GCC as my compiler. I really should get the Intel one shoudn't I? This is what the SRC looks like.

Devan S.
  • 1
  • 4

1 Answers1

0

In C, a library comes in two parts:

  • One (or more) header files
  • The actual library implementation, under Linux either a shared object (.so) or a static library (.a), under Windows I guess it would be a DLL file

You need to include the header file in each C source file of yours where you want to use functionality from the library:

In your source, add a line

#include <library-header.h>

for each header you want to include.

for all header files the library is delivered with. You will have to tell your compiler where to find the header files. For gcc, you would have to enter something like that on the command line.

 gcc -I /path/to/library/directory/include -c my_source.c -o my_source.o

Then, you need to link your program with the actual library. This again, depends on your tool chain, e.g. the compiler you use. For gcc again, the command line would look something like

gcc -lname_of_the_libray  -L/path/to/library/directory/ my_source.o -o my_exe

However, libraries are often distributed in source code, which means you would have to compile the library beforehand.

Michael Beer
  • 853
  • 5
  • 12
  • A few corrections: 1. `.dll`s are used instead of `.so`s only, not `.a`s. 2. `-lblah` should go *after* object files. 3. IIRC there should be no space after `-I`. – HolyBlackCat May 20 '18 at 22:55
  • But say I wanted to be able to build, rebuild ALOT, what would I put in a BAT file to do this for me? And second, is there a way that I can tell a program to auto delete after its run? So maybe later I can make a macro to compile after I'm done editing in my editor (Atom)?, I've edited it to show the inside of the src also – Devan S. May 20 '18 at 22:57
  • Usually, you use some kind of build system, like Make ( https://en.wikipedia.org/wiki/Make_(software)#Derivatives) ). but this is far too much to explain for stackoverflow unfortunately. There are good books about that topic, like for make: http://shop.oreilly.com/product/9780596006105.do – Michael Beer May 20 '18 at 23:00
  • @Devan: you should be using a build system. At a bare minimum, use something like Make. Batch scripts work but they are a bit primitive and inflexible. – Dietrich Epp May 20 '18 at 23:03
  • IDEs often come with own tools integrated for managing/building your code. I never used atom, though... – Michael Beer May 20 '18 at 23:03
  • I should, but for now I'll stick to BAT's, but if you know what I input that would be nice. – Devan S. May 20 '18 at 23:16
  • Also, how would I do that same thing, but with multiple libraries? And is "mysource.c" my current file? and where do I find the .o's? is it in the src folder? – Devan S. May 20 '18 at 23:35
  • "mysource.c" is your source file. If you compile it the way I described above the Compiler will create an .o (its called object file) from it. It will usually be created in the directory you invoked GCC from. Beware: if you invoke GCC not AS I described, it might try to compile & link directly, yielding linking errors. For linking multiple libraries, just add more '-l' arguments: gcc -L/lib/path --lcurl -lglfw my_source.o -o my_exe – Michael Beer May 21 '18 at 07:19
  • @HolyBlackCat The space does not matter - GCC uses GNU conventions. AS for the order of args, it does not matter at least with my version of GCC... – Michael Beer May 21 '18 at 07:21
  • @MichaelBeer I checked it to be sure, and with my GCC order seems to matter in some cases. As for `-I` - thanks, didn't know that. – HolyBlackCat May 21 '18 at 08:02