gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:50:57 25 4
gpt4 key购买 nike

我有三个简单的文件:

类的头文件

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

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

最佳答案

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

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

关于c++ - 在c++ solaris中编译多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13802167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com