我有一个由两个源文件(farm.c,init.c)和两个相应的头文件(farm.h,init.h)组成的程序两个源文件都包含头保护和彼此,因为它们都需要来自的函数/变量彼此。
\n初始化.h:
\n#ifndef INIT_H\n#define INIT_H\n\n#include<stdio.h>\n#include<stdlib.h>\n#include"farm.h"\n\n#define PIG_SOUND "oink"\n#define CALF_SOUND "baa"\n\nenum types {PIG, CALF};\n\ntypedef struct resources {\n size_t pork;\n size_t veal;\n size_t lamb;\n size_t milk;\n size_t eggs;\n} resources;\n\ntypedef struct animal {\n size_t legs;\n char* sound;\n int efficiency;\n void (*exclaim)(struct animal*);\n void (*work)(struct animal*, struct resources*);\n} animal;\n\n/* I have tried various ways of declaring structs in addition to\n the typedef such as this */\n\n//animal stock;\n//animal farm;\n\nvoid make_pig(struct animal* a, int perf);\nvoid make_calf(struct animal* a, int perf);\n\n#endif\nRun Code Online (Sandbox Code Playgroud)\n农场.h:
\n#ifndef FARM_H\n#define …Run Code Online (Sandbox Code Playgroud) 这困扰了我好多年。谁能解释为什么$(HOSTNAME)不扩展到环境值HOSTNAME?谷歌搜索“ make”,“ hostname”,“ gmake”,“ not set”等等的各种组合对我来说并不富有成果。
jcomeau@intrepid:/tmp$ set | egrep 'HOSTNAME|USER'
HOSTNAME=intrepid
USER=jcomeau
jcomeau@intrepid:/tmp$ cat Makefile
%.test:
set | grep $*
%.env:
echo $($*)
jcomeau@intrepid:/tmp$ make HOSTNAME.test
set | grep HOSTNAME
BASH_EXECUTION_STRING='set | grep HOSTNAME'
HOSTNAME=intrepid
jcomeau@intrepid:/tmp$ make HOSTNAME.env
echo
jcomeau@intrepid:/tmp$ make USER.test
set | grep USER
BASH_EXECUTION_STRING='set | grep USER'
USER=jcomeau
jcomeau@intrepid:/tmp$ make USER.env
echo jcomeau
jcomeau
Run Code Online (Sandbox Code Playgroud) Makefile文件:
$(shell ./test.sh)
Run Code Online (Sandbox Code Playgroud)
第一个实验:test.sh
echo "hi"
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
Makefile:1: *** missing separator. Stop.
Run Code Online (Sandbox Code Playgroud)
第二个实验:test.sh
echo("hi")
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
./test.sh: line 1: syntax error near unexpected token `"hi"'
./test.sh: line 1: `echo("hi")'
Run Code Online (Sandbox Code Playgroud)
没有任何意义......看起来'Make'试图将其语法强加于shell脚本,但shell脚本也需要它自己.
我有一个gnu makefile模板,它很好用,但是当我尝试在路径中指定除第一个g ++之外的编译器时,它会失败.
这是模板.
CXX = g++
CXXFLAGS = $(INC) $(LIB) -Wall
INC = -I./ -I/usr/local/include
LIB = -L/usr/local/lib
SRCS = \
blah1.cpp
blah2.cpp
OBJS = $(SRCS:.cpp=.o)
DEPS = $(SRCS:.cpp=.d)
PROG = myprog
$(PROG): $(OBJS)
$(CXX) $(CXXFLAGS) -o $@ $(OBJS)
%.d: %.cpp
@set -e; rm -f $@; \
$(CXX) -MM $(CXXFLAGS) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
debug: CXXFLAGS += -O0 -DDEBUG -ggdb
debug: $(PROG)
-include $(DEPS)
.PHONY: …Run Code Online (Sandbox Code Playgroud) 目前的方式是这样的,看起来很笨拙..
ex1_test : ex1.o ex1_test.o
cc -o ex1_test ex1.o ex1_test.o
ex2_test : ex2.o ex2_test.o
cc -o ex2_test ex2.o ex2_test.o
ex3_test : ex3.o ex3_test.o
cc -o ex3_test ex3.o ex3_test.o
Run Code Online (Sandbox Code Playgroud)
是否有可用于表示当前目标并减少重复的变量?
谢谢!
如何gmake在不修改Makefile?的情况下输出所有命令的退出状态代码?如果修改Makefile是一个选项,可能会出现以下内容:
$(CC) -c -o $@ $< $(CFLAGS); echo $$?
Run Code Online (Sandbox Code Playgroud)
到目前为止,我发现的另一种方法是包装我正在运行的命令.例如,如果
gcc -o $@ $^ $(CFLAGS) $(LIBS)
Run Code Online (Sandbox Code Playgroud)
是运行的命令,当前目录.位于PATH变量的前面,然后您可以gcc使用以下内容创建在当前目录中调用的脚本:
/usr/bin/gcc "$@"; echo $?
Run Code Online (Sandbox Code Playgroud)
我想知道是否有更好,更少hackish,更优雅的解决方案?
直接粘贴到我的shell中,下面尝试3个正则表达式中的每一个并且工作(参见*.{jpg,png,gis.tif}):
for file in ./output/India/*.{jpg,png,gis.tif}; do echo $file; openssl base64 -in $file -out ./output/India/`basename $file`.b64; done;
Run Code Online (Sandbox Code Playgroud)
作为makefile进程,它失败并返回:
task:
for file in ./output/India/*.{png,jpg,gis.tif} ; \
do echo $$file ; openssl base64 -in $$file -out ./output/India/`basename $$file`.b64; \
done
Run Code Online (Sandbox Code Playgroud)
并返回:
47910543179104:error:02001002:system library:fopen:No such file or directory:bss_file.c:398:fopen('./output/India/*.{png,jpg,gis.tif}','r')
Run Code Online (Sandbox Code Playgroud)
为什么当Bash在makefile中时这个表达式不起作用?
我正在尝试遵循本教程:http: //www.cs.colby.edu/maxwell/courses/tutorials/maketutor/
当我在最后一个makefile(#5)时,"make"无法继续(错误提示)No rule to make target "obj/hellomake.o", needed by "hellomake".这段代码试图编译源文件并将libs,srcs,objs放入相应的文件夹中.
IDIR =../include
CC=gcc
CFLAGS=-I$(IDIR)
ODIR=obj
LDIR =../lib
LIBS=-lm
_DEPS = hellomake.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
_OBJ = hellomake.o hellofunc.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
$(ODIR)/%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
hellomake: $(OBJ)
gcc -o $@ $^ $(CFLAGS) $(LIBS)
.PHONY: clean
clean:
rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~
Run Code Online (Sandbox Code Playgroud)
我已经盯着它看了一个小时,这不是一段复杂的代码,但我无法解决这个问题.我怀疑问题发生在这里:
$(ODIR)/%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
hellomake: $(OBJ)
gcc -o $@ $^ …Run Code Online (Sandbox Code Playgroud) 我收到以下错误
make: *** No rule to make target `stretchy_buffer.o', needed by `tsh'. Stop
Run Code Online (Sandbox Code Playgroud)
试图制作这个makefile
SRCS = stretchy_buffer.c def.c tsh_builtin_commands.c tsh_jobs.c tsh_main.c tsh_routines.c tsh_signals.c
OBJS = $(SRCS:.c=.o)
tsh: $(OBJS)
gcc -Wall -g -o tsh $(OBJS)
Run Code Online (Sandbox Code Playgroud) 根据gnu make文档,模式规则的“ ...配方仅执行一次即可创建所有目标。” 但是,我有以下Makefile
.PHONY: entrypoint
entrypoint: test_1.cpp test_2.cpp
test_%.cpp:
echo $@
Run Code Online (Sandbox Code Playgroud)
运行make会产生:
echo test_1.cpp
test_1.cpp
echo test_2.cpp
test_2.cpp
Run Code Online (Sandbox Code Playgroud)
我是新手,可能是我误会了一些东西,但是如果说清楚的话,文档似乎会产生误导。
$ make -v
GNU Make 4.0
...
Run Code Online (Sandbox Code Playgroud) gnu-make ×10
makefile ×9
c ×4
bash ×2
c++ ×1
gcc-warning ×1
pointers ×1
return-value ×1
struct ×1