如何为多个目录中的多个文件创建 Makefile?
How to create Makefile for multiple files in multiple directories?
早上好,
我是 c++ 的新手,我正在尝试将我的简单代码编译为可执行形式。我将解释项目结构。
- main.cpp
- /utility/server.h
- /utility/server.cpp
我附上文件来源以获取完整信息。
main.cpp
#include <iostream>
#include "utility/server.h"
using namespace std;
using namespace server;
int main() {
std::cout << "Content-type:text/html\r\n\r\n";
std::cout << "Your server name is: " << server::get_domain() << '\n';
return 0;
}
server.cpp
#include <cstdlib>
#include "server.h"
namespace server {
static char* get_domain() {
return getenv("SERVER_NAME");
}
}
我在 Makefile 中添加了注释以了解我想要做什么。
#
# 'make' build executable file
# 'make clean' removes all .o and executable files
#
# define the C compiler to use
CC = g++
# define any compile-time flags
CFLAGS = -Wall -g
# define any directories containing header files other than /usr/include
INCLUDES = -I../utility
# define the C++ source files
SRCS = main.cpp utility/server.cpp
# define the C++ object files
OBJS = $(SRCS:.cpp=.o)
# define the executable file
MAIN = executable.cgi
#
# The following part of the makefile is generic
#
.PHONY: depend clean
all: $(MAIN)
$(MAIN): $(OBJS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS)
# this is a suffix replacement rule for building .o's from .cpp's
.c.o:
$(CC) $(CFLAGS) $(INCLUDES) -cpp $< -o $@
clean:
$(RM) *.o *~ $(MAIN)
depend: $(SRCS)
makedepend $(INCLUDES) $^
最后编译出错
g++ -Wall -g -I../utility -o executable.cgi main.o utility/server.o
Undefined symbols for architecture x86_64:
"server::get_domain()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [executable.cgi] Error 1
从错误消息中我了解到 utility
文件夹有问题,但我不知道如何解决它。
感谢您的帮助:)
在server.cpp
,这里:
namespace server {
static char* get_domain() {
return getenv("SERVER_NAME");
}
}
您已将 char* server::get_domain()
设为 static
函数,使
它的定义只在这个翻译单元内可见并且不可见
链接器。删除此处的关键字 static
,如果您在那里声明了函数 static
,也删除 server.h
中的关键字。
A namespace
不是 class
或 struct
。令人困惑的是,
namespace server {
static char* get_domain() {
return getenv("SERVER_NAME");
}
}
server::get_domain()
是该命名空间中的一个 static 函数。但是
struct server {
static char* get_domain() {
return getenv("SERVER_NAME");
}
};
它是一个 全局 函数 class,链接器可以看到它。
早上好, 我是 c++ 的新手,我正在尝试将我的简单代码编译为可执行形式。我将解释项目结构。
- main.cpp
- /utility/server.h
- /utility/server.cpp
我附上文件来源以获取完整信息。
main.cpp
#include <iostream>
#include "utility/server.h"
using namespace std;
using namespace server;
int main() {
std::cout << "Content-type:text/html\r\n\r\n";
std::cout << "Your server name is: " << server::get_domain() << '\n';
return 0;
}
server.cpp
#include <cstdlib>
#include "server.h"
namespace server {
static char* get_domain() {
return getenv("SERVER_NAME");
}
}
我在 Makefile 中添加了注释以了解我想要做什么。
#
# 'make' build executable file
# 'make clean' removes all .o and executable files
#
# define the C compiler to use
CC = g++
# define any compile-time flags
CFLAGS = -Wall -g
# define any directories containing header files other than /usr/include
INCLUDES = -I../utility
# define the C++ source files
SRCS = main.cpp utility/server.cpp
# define the C++ object files
OBJS = $(SRCS:.cpp=.o)
# define the executable file
MAIN = executable.cgi
#
# The following part of the makefile is generic
#
.PHONY: depend clean
all: $(MAIN)
$(MAIN): $(OBJS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS)
# this is a suffix replacement rule for building .o's from .cpp's
.c.o:
$(CC) $(CFLAGS) $(INCLUDES) -cpp $< -o $@
clean:
$(RM) *.o *~ $(MAIN)
depend: $(SRCS)
makedepend $(INCLUDES) $^
最后编译出错
g++ -Wall -g -I../utility -o executable.cgi main.o utility/server.o
Undefined symbols for architecture x86_64:
"server::get_domain()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [executable.cgi] Error 1
从错误消息中我了解到 utility
文件夹有问题,但我不知道如何解决它。
感谢您的帮助:)
在server.cpp
,这里:
namespace server {
static char* get_domain() {
return getenv("SERVER_NAME");
}
}
您已将 char* server::get_domain()
设为 static
函数,使
它的定义只在这个翻译单元内可见并且不可见
链接器。删除此处的关键字 static
,如果您在那里声明了函数 static
,也删除 server.h
中的关键字。
A namespace
不是 class
或 struct
。令人困惑的是,
namespace server {
static char* get_domain() {
return getenv("SERVER_NAME");
}
}
server::get_domain()
是该命名空间中的一个 static 函数。但是
struct server {
static char* get_domain() {
return getenv("SERVER_NAME");
}
};
它是一个 全局 函数 class,链接器可以看到它。