简体   繁体   中英

How can I add a library I downloaded to my C compiler?

I've downloaded CSFML from github and would like to use it in one of my programs. How can I do so? I have a source folder containing some header files. It has three sub-folders.

include
lib
share

CSFML is the C-binding to SFML, which itself is a cross-platform multimedia framework.

Instructions for macOS

The instructions differ depending on the platform. Also whether you want to install them system-wide or have the bindings in a separate folder (eg if you play around with different versions of the bindings). There are also many different ways to use it from different development environments or build systems.

Here for example instructions on how to use it on the command line under macOS. The following instructions may therefore not fit your needs exactly, but should at least make the start easier.

Install SFML

Because CSFML is only a C binding to SFML, you must first download and install SFML.

For macOS you can get it from here: https://www.sfml-dev.org/files/SFML-2.5.1-macOS-clang.tar.gz

Due to https://www.sfml-dev.org/tutorials/2.5/start-osx.php#installing-sfml you need to:

  • Copy the content of Frameworks to /Library/Frameworks...
  • Copy the content of extlibs to /Library/Frameworks

Project Structure

There are also different ways to set up a project structure, eg you could simply put your include, lib, and share directory in a subdirectory with the actual name csfml .

Then create a test.c file.

Building

You can then build your test program like this:

gcc -Wall -Wextra -I./csfml/include -L./csfml/lib test.c -lcsfml-graphics -lcsfml-window -o test

-I tells the compiler to look for include files in csfml/include, while -L tells the linker to look for the csfml-graphics and csfml-window libraries in the csfml/lib folder.

Dynamic Link Path

If you try to run the./test executable, it will tell you that it cannot find libcsfml-graphics.dylib. To allow the dynamic linker to find the library at runtime, you can specify the path as follows:

export DYLD_LIBRARY_PATH=./csfml/lib

Then a call to./test would actually run the program under macOS.

Test Program

For the sake of completeness: a simple test program drawing a red circle with a white border on a black background would look like this:

#include <SFML/Window.h>
#include <SFML/Graphics.h>

int main() {
    sfVideoMode mode = {640, 640, 32};
    sfRenderWindow *window = sfRenderWindow_create(mode, "csfml quick test", sfResize | sfClose, NULL);
    sfCircleShape *circle = sfCircleShape_create();
    sfCircleShape_setRadius(circle, 320.0f);
    sfCircleShape_setFillColor(circle, sfRed);
    sfCircleShape_setOutlineColor(circle, sfWhite);
    sfCircleShape_setOutlineThickness(circle, 1.0f);
    while (sfRenderWindow_isOpen(window)) {
        sfEvent event;
        while (sfRenderWindow_pollEvent(window, &event)) {
            if (event.type == sfEvtClosed) {
                sfRenderWindow_close(window);
            }
        }
        sfRenderWindow_clear(window, sfBlack);
        sfRenderWindow_drawCircleShape(window, circle, NULL);
        sfRenderWindow_display(window);
    }
    return 0;
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM