Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 一个用于2个cpp程序的生成文件_C++_Makefile - Fatal编程技术网

C++ 一个用于2个cpp程序的生成文件

C++ 一个用于2个cpp程序的生成文件,c++,makefile,C++,Makefile,您好,我需要为一个目录中的两个独立的cpp程序创建一个makefile。我有这个代码,但它不能正常工作。无法创建.o文件。谢谢 OBJS = a b EXEC = first_run second_run #------ constant definitions ALL_OBJ = $(OBJS:%=%.o) all: $(EXEC) clean: $(RM) $(EXEC) $(OBJS) $(ALL_OBJ); make all CC = g++ DO_OBJS = $(

您好,我需要为一个目录中的两个独立的cpp程序创建一个makefile。我有这个代码,但它不能正常工作。无法创建.o文件。谢谢

OBJS = a b
EXEC = first_run second_run

#------ constant definitions

ALL_OBJ = $(OBJS:%=%.o)

all: $(EXEC)

clean:
    $(RM) $(EXEC) $(OBJS) $(ALL_OBJ); make all

CC = g++

DO_OBJS = $(CC) -cpp -o $@.o $@.cpp; touch $@
DO_EXEC = $(CC) -s -o $@ $(ALL_OBJ)

#------ now compile

$(OBJS):    $(@:%=%.o)
        $(DO_OBJS)

$(EXEC):    $(OBJS)
        $(DO_EXEC)

您的文件有几个问题,但主要的问题似乎是您试图将两个源文件链接到单个可执行文件。您必须单独列出每个程序及其依赖项

请尝试以下简单的生成文件:

SOURCES = a.cpp b.cpp
OBJECTS = $(SOURCES:%.cpp=%.o)
TARGETS = first_run second_run

LD = g++
CXX = g++
CXXFLAGS = -Wall

all: $(TARGETS)

# Special rule that tells `make` that the `clean` target isn't really
# a file that can be made
.PHONY: clean

clean:
    -rm -f $(OBJECTS)
    -rm -f $(TARGETS)

# The target `first_run` depends on the `a.o` object file
# It's this rule that links the first program
first_run: a.o
    $(LD) -o $@ $<

# The target `second_run` depends on the `b.o` object file
# It's this rule that links the second program
second_run: b.o
    $(LD) -o $@ $<

# Tells `make` that each file ending in `.o` depends on a similar
# named file but ending in `.cpp`
# It's this rule that makes the object files    
.o.cpp:
EXEC = first_run second_run
OBJS_FIRST = a.o
OBJS_SECOND = b.o

all: $(EXEC)

first_run: $(OBJS_FIRST)
    $(CXX) -o $@ $(OBJS_FIRST)

second_run: $(OBJS_SECOND)
    $(CXX) -o $@ $(OBJS_SECOND)
SOURCES=a.cpp b.cpp
对象=$(源:%.cpp=%.o)
目标=第一次运行第二次运行
LD=g++
CXX=g++
CXXFLAGS=-Wall
全部:$(目标)
#告诉“make”这个“clean”目标实际上不是
#可以制作的文件
.假冒:干净
清洁:
-rm-f$(对象)
-rm-f$(目标)
#目标'first_run'依赖于'a.o'对象文件
#正是这条规则链接了第一个程序
第一轮:a.o
$(LD)-o$@$<
#目标'second_run'依赖于'b.o'对象文件
#这条规则连接了第二个程序
第二轮:b.o
$(LD)-o$@$<
#告诉'make'每个以'.o'结尾的文件都依赖于一个类似的
#命名文件,但以“.cpp”结尾`
#正是这个规则使对象文件
.o.cpp:

您的文件有几个问题,但主要问题似乎是您试图将两个源文件链接到单个可执行文件。您必须单独列出每个程序及其依赖项

请尝试以下简单的生成文件:

SOURCES = a.cpp b.cpp
OBJECTS = $(SOURCES:%.cpp=%.o)
TARGETS = first_run second_run

LD = g++
CXX = g++
CXXFLAGS = -Wall

all: $(TARGETS)

# Special rule that tells `make` that the `clean` target isn't really
# a file that can be made
.PHONY: clean

clean:
    -rm -f $(OBJECTS)
    -rm -f $(TARGETS)

# The target `first_run` depends on the `a.o` object file
# It's this rule that links the first program
first_run: a.o
    $(LD) -o $@ $<

# The target `second_run` depends on the `b.o` object file
# It's this rule that links the second program
second_run: b.o
    $(LD) -o $@ $<

# Tells `make` that each file ending in `.o` depends on a similar
# named file but ending in `.cpp`
# It's this rule that makes the object files    
.o.cpp:
EXEC = first_run second_run
OBJS_FIRST = a.o
OBJS_SECOND = b.o

all: $(EXEC)

first_run: $(OBJS_FIRST)
    $(CXX) -o $@ $(OBJS_FIRST)

second_run: $(OBJS_SECOND)
    $(CXX) -o $@ $(OBJS_SECOND)
SOURCES=a.cpp b.cpp
对象=$(源:%.cpp=%.o)
目标=第一次运行第二次运行
LD=g++
CXX=g++
CXXFLAGS=-Wall
全部:$(目标)
#告诉“make”这个“clean”目标实际上不是
#可以制作的文件
.假冒:干净
清洁:
-rm-f$(对象)
-rm-f$(目标)
#目标'first_run'依赖于'a.o'对象文件
#正是这条规则链接了第一个程序
第一轮:a.o
$(LD)-o$@$<
#目标'second_run'依赖于'b.o'对象文件
#这条规则连接了第二个程序
第二轮:b.o
$(LD)-o$@$<
#告诉'make'每个以'.o'结尾的文件都依赖于一个类似的
#命名文件,但以“.cpp”结尾`
#正是这个规则使对象文件
.o.cpp:

我建议使用以下Makefile:

SOURCES = a.cpp b.cpp
OBJECTS = $(SOURCES:%.cpp=%.o)
TARGETS = first_run second_run

LD = g++
CXX = g++
CXXFLAGS = -Wall

all: $(TARGETS)

# Special rule that tells `make` that the `clean` target isn't really
# a file that can be made
.PHONY: clean

clean:
    -rm -f $(OBJECTS)
    -rm -f $(TARGETS)

# The target `first_run` depends on the `a.o` object file
# It's this rule that links the first program
first_run: a.o
    $(LD) -o $@ $<

# The target `second_run` depends on the `b.o` object file
# It's this rule that links the second program
second_run: b.o
    $(LD) -o $@ $<

# Tells `make` that each file ending in `.o` depends on a similar
# named file but ending in `.cpp`
# It's this rule that makes the object files    
.o.cpp:
EXEC = first_run second_run
OBJS_FIRST = a.o
OBJS_SECOND = b.o

all: $(EXEC)

first_run: $(OBJS_FIRST)
    $(CXX) -o $@ $(OBJS_FIRST)

second_run: $(OBJS_SECOND)
    $(CXX) -o $@ $(OBJS_SECOND)

您不需要定义对象构建,因为make已经知道如何进行该操作。

我建议使用以下Makefile:

SOURCES = a.cpp b.cpp
OBJECTS = $(SOURCES:%.cpp=%.o)
TARGETS = first_run second_run

LD = g++
CXX = g++
CXXFLAGS = -Wall

all: $(TARGETS)

# Special rule that tells `make` that the `clean` target isn't really
# a file that can be made
.PHONY: clean

clean:
    -rm -f $(OBJECTS)
    -rm -f $(TARGETS)

# The target `first_run` depends on the `a.o` object file
# It's this rule that links the first program
first_run: a.o
    $(LD) -o $@ $<

# The target `second_run` depends on the `b.o` object file
# It's this rule that links the second program
second_run: b.o
    $(LD) -o $@ $<

# Tells `make` that each file ending in `.o` depends on a similar
# named file but ending in `.cpp`
# It's this rule that makes the object files    
.o.cpp:
EXEC = first_run second_run
OBJS_FIRST = a.o
OBJS_SECOND = b.o

all: $(EXEC)

first_run: $(OBJS_FIRST)
    $(CXX) -o $@ $(OBJS_FIRST)

second_run: $(OBJS_SECOND)
    $(CXX) -o $@ $(OBJS_SECOND)
您不需要定义对象构建,因为make已经知道如何进行该操作。

KISS:

all: first_run second_run

clean:
    rm -f first_run second_run

first_run: a.c
    $(LINK.cc) $^ $(LOADLIBES) $(LDLIBS) -o $@

second_run: b.c
    $(LINK.cc) $^ $(LOADLIBES) $(LDLIBS) -o $@
接吻:


g++-s-o首先运行a.o b.o a.o:在函数
\u start':(.text+0x0):多个定义
\u start'/usr/lib/gcc/i686 linux gnu/4.6/../../../../../i386 linux gnu/crt1.o:(.text+0x0):首先在这里定义a.o:(.rodata+0x0):多个定义
\u fp uhw'/usr/lib/gcc/i686 linux gnu/4.6/../../../i386 linux gnu+0x0:(:首先在这里定义a.o:In function
\u fini:(.fini+0x0)“\u fini”/usr/lib/gcc/i686 linux gnu/4.6/../../../../../i386 linux gnu/crti.o:(.fini+0x0):首先在这里定义你想实现什么?
a
b
如何与
第一次运行
第二次运行
相关?您的第一个程序应该是名为第一次运行的可执行文件吗?或者一个名为
a
的可执行文件?还是什么?我想从一个make文件创建a.o和b.o。第一次运行用于a,第二次运行用于b。您只需要
a.o
b.o
?您不想创建一个可执行文件吗?g++-s-o first\u run a.o b.o a.o:In function
\u start':(.text+0x0):多个
\u start'/usr/lib/gcc/i686 linux gnu/4.6/../../../../i386 linux gnu/crt1.o:(.text+0x0):首先在这里定义a.o:(.rodata+0x0):多定义
\u fp\u hw'/usr/lib/gcc/i686 linux gnu/4.6/../../../../i386 linux gnu/crt1.o:(.rodata+0x0):首先在这里定义a.o:在函数中(.fini+0x0):(.fini+0x0):多定义`u fini'/usr/lib/gcc/i686 linux gnu/4.6/../../../../i386 linux gnu/crti.o:(.fini+0x0):首先定义这里,您试图实现什么?
a
b
如何与
第一次运行
第二次运行
相关?您的第一个程序应该是名为第一次运行的可执行文件吗?或者一个名为
a
的可执行文件?还是什么?我想从一个make文件创建a.o和b.o。第一次运行用于a,第二次运行用于b。您只需要
a.o
b.o
?您不想创建可执行文件?@NatashaLevesque忘记添加链接命令,请查看我修改的答案。makefile:16:**缺少分隔符。停,嗯。。。仍然是相同的错误makefile:16:**缺少分隔符。停下来,它起作用了。将-rm-f$(OBJECTS)-rm-f$(TARGETS)更改为\rm-f$(OBJECTS)\rm-f$(TARGETS)@NatashaLevesque忘记添加链接命令,请查看我修改的答案。makefile:16:**缺少分隔符。停,嗯。。。仍然是相同的错误makefile:16:**缺少分隔符。停下来,它起作用了。将-rm-f$(对象)-rm-f$(目标)更改为\rm-f$(对象)\rm-f$(目标)