简体   繁体   中英

how to add a library to 'make' command in c

This is the header file and its C file: cs50.h and cs50.c

Now I use them in the following example http://www.paste.ubuntu.com/576370/ — which is no longer available.

I already put the header file in /usr/bin/include or something like that and when I try to compile my code using gcc -o xxx xxx.c , it doesn't work, so tried to fix this and the following way worked: http://www.paste.ubuntu.com/576371/ — which is no longer available.

Now I want to do something to make the 'make' command work as the gcc does.

What do I need to do?


The following was the old topic:

I was using gcc command to compile C programs but after a period of time I got a problem. I need to compile a new header file and use it as a library.

The header file called cs50.h .

so after doing it and it's ok I can compile using the following

gcc -o xxx xxx.c -lcs50

It works but now I want to use 'make' command and I can't get it to work.

It just don't compile the header file and library as gcc was before I edit it to accept the cs50 library.

So now I want to add to the 'make' command the following: -lcs50

Can anyone help me in this please?

Near the top of your Makefile, add the line:

LDLIBS = -lcs50

If you are using Make's default (implicit) rules for the building, then that is all you need to do. If you are using explicit rules, you will need to add $(LDLIBS) to your explicit rules.

If there is no Makefile, then make is using default rules, and you can either just create a makefile with

echo LDLIBS = -lcs50 > Makefile

or tell make to link with certain libraries by specifying the required libraries in LDLIBS in the environment. For example, if you are using a sh-derived shell (anything other than csh or tcsh) you can do:

LDLIBS=-lcs50 make target

If you are using a csh variant, you can do:

env LDLIBS=-lcs50 make target

or just do (again, for non-csh shells)

export LDLIBS=-lcs50

before running make. (For csh variants, do setenv LDLIBS -lcs50)

You can use below “make” command to link library and include header directories,

make <.c or .cpp source filename_without_extension> LDLIBS="-l<lib1> -l<lib2>"

suppose you have server.cpp file to compile using make command,

make server LDLIBS="-lcpprest -lpthread -lssl -lcrypto" LDFLAGS="-L/usr/lib/" CXXFLAGS="-I/usr/include/"

Output will expand the compilation command as,

g++ -I/usr/include/  -L/usr/lib/  server.cpp  -lcpprest -lpthread -lssl -lcrypto -o server

Did you forget that you have to tell gcc in what directory the CS50 library is located?

gcc … -L/directory/for/cs50_library -lcs50

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