双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

linux下连接静态库的问题

yjyj985jie yjyj985jie 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
用静态库封装类调用时出错,不明白问题出在哪里,请高手指点。简单示例程序:

#ifndef __HTTP_H__
#define __HTTP_H__
#include <stdio.h>
class Http
{
public:
void InitHttp();
void CloseHttp();
int PrintHttp(const char* str);
};
#endif

cpp文件:
#include "http.h"


void Http::InitHttp()
{

}

void Http::CloseHttp()
{

}

int Http::PrintHttp(const char* str)
{
printf("%s\n",str);
return 1;
}

main.cpp
#include "http.h"

int main()
{
Http* m_http = new Http();
m_http->PrintHttp("http");

delete m_http;
return 1;
}

先生成目标文件: g++ -c http.cpp
生成静态库: ar cr libmyhttp.a http.o
这两步什么问题,都已经生成,连接后编译:
g++ -o test main.cpp -L.-lmyhttp
出现错误:/tmp/cc6cxeOn.o: In function `main':
main.cpp:(.text+0x20): undefined reference to `Http::PrintHttp(char const*)'
collect2: ld 返回 1
本人刚接触linux下的编程,请高手指点。
...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
yjyj985jie
Linux/Unix社区 23,224 成员 74,535 内容