Reputation: 238617
I am getting started with C programming and am trying to learn how to use other C libraries. I'm trying to get the glfw example running, but having trouble linking/including the library. I have downloaded the library, which exists in a separate directory. I'm also a little confused as to which command I should be using: make
, cmake
, cc
, gcc
, etc. How should I go about compiling this library with my C program?
Update: Sorry if I was not clear. The answer to this question should be pretty simple. I'm on a Mac. I am intentionally not using an IDE. Here is the script that I linked to above:
#include <GLFW/glfw3.h>
int main(void)
{
/* code here, not relevant to question */
}
I downloaded the library, and unzipped it, it looks something like this:
glfw-3.0.2/
├── CMake
├── CMakeFiles
├── deps
├── docs
├── examples
├── include
├── src
├── Makefile
├── README.md
In the readme, it says to build it using cmake like this:
cd glfw-3.0.2
cmake .
The output of this command finished with this statement:
-- Build files have been written to: /Users/andrew/mydirectory/myproj/glfw-3.0.2
My script lives in a directory one level higher than the glfw-3.0.2
directory.
Now when I try to compile my script, I need to tell the compiler how to include the library, but that's the part that I don't know how to do:
$ make myscript
cc myscript.c -o myscript
myscript.c:1:10: fatal error: 'GLFW/glfw3.h' file not found
#include <GLFW/glfw3.h>
^
1 error generated.
make: *** [myscript] Error 1
How can I tell the compiler where to find the library? Also, I assume that once I compile it, I don't need to be concerned about keeping the original library directory since it will have been "bundled" with my script. Is that assumption correct?
Upvotes: 2
Views: 2433
Reputation: 238617
In case this is helpful for anyone finding this post, I'm going to summarize the things that I've learned related to building/compiling third-party C libraries.
cmake
is a tool for building Makefiles
.
make
is a tool for passing configuration options to the compiler using a Makefile
gcc
and cc
are two different compiler implementations. Their command-line flag arguments are similar, but again, they are different implementations so their flags may vary.
Here is a summary of the important flags:
-I (capital i) Include a directory of header files.
-L Include a library directory.
-l (lowercase L) Link to a library that has already been compiled.
Libraries that you compile and install system-wide (using make install
) become available to the compiler automatically because the library has been placed in a location that the compiler looks when including libraries. This means you don't have to use the -I
and -L
flags.
When you compile your own program using libraries that are installed system-wide, you will only need to pass the -l
flag to link to the name of the library. In my case it looks like this:
cc myscript.c -lglfw3
Upvotes: 1
Reputation: 6021
Just running cmake .
doesn't actually build or install the library. It just creates and configures the build files needed to build the library. Typically you'll need to run the commands:
cmake .
make
sudo make install
By default cmake/make will assume that the destination for installation will be the /user directory, with components going into /usr/bin, /usr/lib, and /usr/include. This is why you have to run 'make install' via sudo. If you have sudo permission this is probably the easiest path to take because it will put the library and include files where they will be found automatically when you go to compile your program.
If you don't want to install GLFW into /usr you can specify the install directory with arguments to cmake. For example:
cmake -DCMAKE_INSTALL_PREFIX:PATH=$HOME/glfw .
However, if you do this, it will now be up to you to specify in the makefile for your program where to find the library and the include files. This is done using the cc -I flag to specify the path to the include files, the cc -L flag to specify the location of the library, and the cc -l flag to specify the library to link to. For example, if you've installed the GLFW library in your home directory:
cc -I $HOME/glfw/include -L $HOME/glfw/lib -o myscript myscript.c -lglfw
Upvotes: 2
Reputation: 23208
The error you are describing is consistent in concept to what they are showing HERE. Have you tried including the directory containing the .h file like this: `gcc -I../GLFW ...', where I believe the '../' should take you up one directory relative to your script location.
Also, regarding bundling, The same rules will apply to your .exe after you build. That is, any .dlls your .exe depends on will need to be visible (eg. in the system directory, or in the local directory) to the .exe. I do not think that this happens automatically, but there is a gcc -bundle option you can read about.
Upvotes: 1