// Solution:
// foo.c includes foo.h; bar.c includes bar.h
g++ -Wall -Wextra -Werror -pedantic -o foobar foo.cpp bar.cpp
我有两个cpp文件和两个H文件。我有第一个cpp链接到相同名称的h文件链接到第二个cpp文件链接到第二个h文件。这是我应该将文件链接在一起的方式吗?
我的程序不工作。也有错误,但我需要确定这是否是错误的一部分。这是定义/链接cpp和h文件的方式吗?
// file: foo.cpp
#include "foo.h"
// file: foo.h
#include "bar.cpp"
#include <string>
#include <sstream>
#include <iostream>
using std::cin;
using std::cout;
using std::cerr;
using std::stringstream;
using std::string;
using std::endl;
// file: bar.cpp
#include "bar.h"
// file: bar.h
// ...
这看起来公然错了:'#include“bar.cpp”'通常你永远不会包含'.cpp'文件。将每个'.cpp'文件编译为'.o'或'.obj',并调用链接器从所有这些目标文件创建一个可执行文件。 –
你可能想看看http://david.rothlis.net/c/compilation_model/ –