如何为Linux下的GCC编写适配mycode项目的简单Makefile?
Simple Makefile for Your libstrophe Project
Got it, let's turn that manual gcc command into a reusable Makefile that's easy to maintain. Here's a straightforward implementation that matches your compilation setup:
# Define the compiler to use CC = gcc # Compiler flags: include libstrophe's compile flags via pkg-config CFLAGS = `pkg-config --cflags libstrophe` # Linker flags: include libstrophe's libraries + pthread LIBS = `pkg-config --libs libstrophe` -lpthread # The target executable TARGET = mycode # Build the target when running `make` $(TARGET): mycode.c mycode.h $(CC) $(CFLAGS) -o $@ $< $(LIBS) # Clean up the executable when running `make clean` .PHONY: clean clean: rm -f $(TARGET)
How it works:
CC: Sets the compiler togcc, which matches your original command.CFLAGS: Captures libstrophe's required compile flags usingpkg-config --cflags libstrophe, just like your manual command.LIBS: Combines libstrophe's libraries (pkg-config --libs libstrophe) with thepthreadlibrary you need.$(TARGET): The main rule tells Make to buildmycodefrom your source and header files.$@refers to the target (mycode), and$<refers to the first dependency (mycode.c).clean: A helper rule to delete the compiled executable, useful for cleaning up your project directory. The.PHONYline ensures Make doesn't confuse a file namedcleanwith the rule.
Usage:
- Run
makein your project directory to compile your code into themycodeexecutable. - Run
make cleanto remove themycodeexecutable.
This Makefile keeps things simple while staying true to your original compilation command, and it's easy to tweak later if you add more source files or need to adjust flags.
内容的提问来源于stack exchange,提问作者Abdeslam ben issa
相关产品推荐
相关产品推荐

