在c ++ solaris中编译多个文件

时间:2012-12-10 13:43:45

标签: c++ compiler-construction compilation solaris

我有三个简单的文件:

类的头文件

> cat myhh.hh
#include<iostream>

class helloworld
{
string str;
public:
void start(string &);
void stop(string &);
};

其对应的cc文件

> cat myhh.cc
#include<iostream>
#include "myhh.hh"
using namespace std;

void helloworld::start(string &str1)
{
  cout<< ""function started"<<endl;
}
void helloworld::stop(string &str2)
{
cout<< ""function stopped"<<endl;
}

现在主要功能:

> cat mymain.cc 
#include<iostream>

#include "myhh.hh" 

int main()
{

helloworld obj;
obj.start(std::string("XXXX"));
obj.stop(std::string("XXXX"));


}
>

我的主要内容是使用这3个文件生成a.out,当我执行它时应该打印:

function started
function stopped

我试图编译但是我遇到了错误。

> CC mymain.cc
Undefined                       first referenced
 symbol                             in file
void helloworld::stop(std::basic_string<char,std::char_traits<char>,std::allocator<char> >&) mymain.o
void helloworld::start(std::basic_string<char,std::char_traits<char>,std::allocator<char> >&) mymain.o
ld: fatal: Symbol referencing errors. No output written to a.out
> 

我确信我没有正确的基础知识。但我只是想学习编译过程。 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

您需要编译myhh.cc并链接生成的对象

$CC -c myhh.cc -o myhh.o
$CC -o mymain mymain.cc myhh.o