读书人

程序编译有关问题

发布时间: 2012-03-07 09:13:51 作者: rapoo

程序编译问题!
我写了两个cpp文件:

data10_01.cpp

#include <string>
using std::string;

int count;
float phi=1.618f;
extern const double pi=3.14159265;
extern const string days[]={ "Sunday ", "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday "};

prog10_01.cpp

#include <iostream>
#include <string>
#include <iomanip>
using std::cout;
using std::endl;
using std::string;

extern float phi;
extern const double pi;
extern const string days[];
extern int count;

int main()
{
cout < <std::setprecision(3) < <std::fixed;
cout < <endl
< < "To 3 decimal places... " < <endl;
cout < < "...a circle with a diameter of phi has an area of "
< <pi*phi*phi/4
< <endl;
cout < < "...phi squared is " < <phi*phi < <endl;
cout < < "...in fact,phi+1 is also " < <phi+1 < <endl;
cout < < "Value of count is " < <count < <endl;
count+=3;
cout < < "Today is " < <days[count] < <endl;
return 0;
}

我将data10_01.cpp编译成obj文件,却提示:error LNK2001:unresolved external symbol _main
                 :fatal error LNK1120: 1 unresolved externals
编译prog10_01.cpp文件时,提示更多。

请问各位高手,如何编译这两个cpp文件?

[解决办法]
你要想编译成中间文件.obj,要用参数的!你看一下TC的帮助吧。默认情况下TC会把.cpp文件编译成.exe文件,你得data10_01.cpp没有main函数,所以报错!
你的第二个文件报错更多,是因为你用到了data10_01.cpp文件中定义的变量,但是你有没有将data10_01.cpp包含进去。
总而言之,是可以将各个.cpp文件先编译,再连接;前提是要各个.cpp文件都能独立编译通过。

读书人网 >C++

热点推荐