// 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/ –