Daniele Daniele - 3 months ago 24
C Question

add library on makefile

I execute the guide of "Using a “Real” Cross-Compiler" descript on the link. In the botton of the guide there is a standard makefile. I write a simple program write in C and with the comand make it run. Now I would to write a program with the library "ev3c.h". How can I add this library in the makefile?

Answer

If you're using gcc or clang, pass -I /home/daniele/ev3c/include to it in your recipe. That'll allow you to #include from that path with #include "someheader".

As far as Make-treatment of include files is concerned, they're usually implicit dependencies—that is you usually don't want to mention them in your dependencies explicitly, because compilers such as clang or gcc can detect them and add them to the appropriate Makefile rules for you.

-MMD -MF somefile allows you to do this kind of #include dependency detection with gcc or clang:

For example:

%.o: %.c
    $(COMPILE.c) -I /home/daniele/ev3c/include $(OUTPUT_OPTION) $< -MMD -MF $(patsubst %.o,%.o.d,$@)

-include $(wildcard *.o.d)

This will have the compiler detect include dependencies for each compiled c file and the -include line will add these dependencies to your Makefile so that if your C file includes "foo.h" and foo.h changes, make will trigger a recompile even if you don't mention foo.h as an explicit dependency.

Comments