简体   繁体   中英

how do I import a c library?

I'm new to programming and am taking the cs50 online course, the course provides an online container with an IDE but in order to do the problem sets offline i downloaded the library files but haven been able to reference them on my code, the library import statement is declared as not used and the function from that library is marked as non existent, could anyone lend a helping hand? print from the issue

Download all the files, I suppose they are cs50.h and cs50.c .

Put both files in the same directory of your main file, and use include statement for cs50.h like this:

#include "cs50.h"

When we use a library that is not in the standard library folder, we must include it with "" instead of <>

Note by editor

The above statement is stricken because it's misleading. You can in fact use <> to include your own headers, provided you pass the directory in which those headers reside as one of the search paths to your compiler.

Let's say you want to compile foo.c that uses a header file called bar.h residing in /where/bar/lives/include/ directory, and a library called libbar.a in /where/bar/lives/lib/ directory, then in majority of C compilers you can use -I flag and -L flags to make it possible to include and link the right bits into your project:

To compile your program foo.c you would:

cc -I/where/bar/lives/include -o foo.o -c foo.c

To link you would:

cc -o foo foo.o -L/where/bar/lives/lib -lbar

These two steps would produce your program binary foo

Interestingly you can use -I. and -L. to include present working directories and use <> to your heart's content.

First off, the mechanism is called include in C, as the code itself suggests.

Then, your issue is in the #include statement. Using <...> tells the compiler (specifically the preprocessor) to look for libraries installed in your system. To include local libraries you should use "..." . When using this, also pay attention to the path because it's relative. So, considering your folder structure, the include statement should be

 #include "src/cs50.h"

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