g++如何使用make

g++ how to use make?

本文关键字:make 何使用 g++      更新时间:2023-10-16

我有这个生成文件

CC = g++
CFLAGS = -std=c++14 -Wall -Wpedantic -g
PROG = a
OBJS = other.o main.o
SRCS = other.cpp main.cpp
a: $(OBJS)
    $(CC) $(CFLAGS) -o $(PROG) $(OBJS)
.cpp.o:
    $(CC) $(CFLAGS) -c $*.cpp
clean:
    $(RM) -f $(OBJS) $(PROG)
depend:
    makedepend -- $(CFLAGS) -- $(SRCS)

我的另一个.cpp是

int f( ) noexcept // example function
{
    return 2;
}

我的主.cpp是

int main( int, char** )
{
    f( );
    return 0;
}

所以,很明显,当我运行make depend时,它只添加了一行#DO NOT DELETE。然而,在编译时,当我只运行make时,我会得到main.cpp:‘f’ was not declared in this scope的错误。我想我错过了一件大事。有人能解释一下为什么这个不编译以及我应该怎么做吗?

这与Makefile无关。

编译器的错误消息是不言自明的。在C++中,函数在使用之前必须声明。

添加适当的声明:

int f() noexcept;

致您的main.cpp