当前位置: 技术问答>linux和unix
linux下连接静态库的问题
来源: 互联网 发布时间:2016-07-28
本文导语: 用静态库封装类调用时出错,不明白问题出在哪里,请高手指点。简单示例程序: #ifndef __HTTP_H__ #define __HTTP_H__ #include class Http { public: void InitHttp(); void CloseHttp(); int PrintHttp(const char* str); }; #endif cpp文件: ...
用静态库封装类调用时出错,不明白问题出在哪里,请高手指点。简单示例程序:
#ifndef __HTTP_H__
#define __HTTP_H__
#include
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("%sn",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下的编程,请高手指点。
#ifndef __HTTP_H__
#define __HTTP_H__
#include
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("%sn",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下的编程,请高手指点。
|
g++ -o test main.cpp -L./ -lmyhttp
|
lz 先确保不使用静态库的时候能编译运行成功先
#include 不规范
delete m_http; //这个语法没错?
#include 不规范
delete m_http; //这个语法没错?