c++的.o文件的链接顺序
linker对链接顺序要求很严格,如果顺序有误,多半就会报undefined reference to xxxxxx的错误
文件目录:
代码:
main.cpp
#include "Test.h" using namespace std; int main()
{
Test::testLiftOff();
return ;
}
Test.cpp
#include "Test.h" #include "LiftOff.h" #include <zthread/Thread.h> #include <iostream> // std::cout void Test::testLiftOff()
{
using namespace ZThread; try {
for (int i = ; i < ; ++i)
{
Thread th(new LiftOff(, i));
}
std::cout << "waiting for lift off" << std::endl;
} catch (Synchronization_Exception &e) {
std::cerr << e.what() << std::endl;
}
} Test::Test()
{
//ctor
} Test::~Test()
{
//dtor
}
LiftOff.cpp
#include "LiftOff.h" #include <iostream> using namespace std; LiftOff::LiftOff(int countDown_, int id_)
:countDown(countDown_), id(id_)
{
// do nothing
} LiftOff::~LiftOff()
{
cout << "LiftOff" << id << " destroyed" << endl;
} void LiftOff::run()
{
while (countDown--)
cout << id << " count: " << countDown << endl;
cout << id << "LiftOff!" << endl;
}
Test.h
#ifndef TEST_H
#define TEST_H class Test
{
public:
static void testLiftOff(); private:
Test();
~Test();
}; #endif // TEST_H
LiftOff.h
#ifndef LIFTOFF_H
#define LIFTOFF_H #include <zthread/Runnable.h> class LiftOff : public ZThread::Runnable
{
public:
LiftOff(int countDown_, int id_);
~LiftOff();
void run();
private:
int countDown;
int id;
}; #endif // LIFTOFF_H
显然,main.cpp 通过 Test.h 引用 Test.cpp 的 implementation 。 说人话? 好吧 。。。 具体来说就是linker在发现main.cpp 中的 Test::testLiftOff() 调用的时候, 需要去找Test::testLiftOff()的implementation,去哪找?当然是Test.cpp中,编译为obj文件后,其实就是main.o 依赖于 Test.o
我们把这种关系描述为 main.o < Test.o
类似的还有 Test.o < LiftOff.o 、 Test.o < zthread_win3.a
总的原则就是:如果A.o依赖于B.o,那么在linker命令中,A.o必须在B.o的左边(可以不相邻)
所以在链接的时候,命令为:
g++ -o test.exe main.o Test.o LiftOff.o -s zthread_win32.a // linker command 1
其实,只要顺序不违反上面的关系定义,后面的顺序是可以任意调整的,例如,实际上LiftOff.o与-s zthread_win3.a的顺序调一下也是可以的
g++ -o test.exe main.o Test.o -s zthread_win32.a LiftOff.o // linker command 2
总结:
你编译上面的代码为:
g++ -c *.cpp
链接的时候上面给出的linker command 1、linker command 2任意选一个都行
另外:
事实上,在linux下,由于ZThread依赖于Posix Thread Library,在 -s zthread_xxx.a之后还需要-lpthread来加载Posix Thread Library,也是这个原因,见这篇随笔
c++的.o文件的链接顺序的更多相关文章
- Linux链接库四(多个库文件链接顺序问题)
最近在Linux下编程发现一个诡异的现象,就是在链接一个静态库的时候总是报错,类似下面这样的错误: (.text+0x13): undefined reference to `func' 关于unde ...
- gcc/g++动态链接库和静态库的链接顺序
转自:http://withc8212.blog.163.com/blog/static/11656983820109263562854/ so文件:动态库a文件: 静态库exe文件:可执行程序(li ...
- gcc中动态库和静态库的链接顺序
so文件:动态库a文件: 静态库exe文件:可执行程序(linux下以文件属性来标示是否是可执行文件,与后缀名无关) 经过自己写的一些测试程序,大致了解了下gcc中链接顺序问题,总结出以下几点:1,动 ...
- C++头文件的包含顺序研究
一.<Google C++ 编程风格指南>里的观点 公司在推行编码规范,领导提议基本上使用<Google C++ 编程风格指南>.其中<Google C++ 编程风格指南 ...
- 保存chrome书签中链接顺序的小技巧
长期以来,我的chrome浏览器书签总是不能自主排序,用书签管理器排序之后,不关闭chrome是好的,一旦关闭重开,又会恢复成原先的排序,实在很诧异,手动修改Bookmars文件也不行. 我们知道,如 ...
- Linux ln命令 - 建立文件/目录链接
转自Linux ln命令 - 建立文件/目录链接 1. 使用方式:ln [option] source_file dist_file -f 建立时,将同档案名删 ...
- Beyond Compare 忽略两个文件内容的顺序比较文件内容(xjl456852原创)
有时两个文件内容的顺序是不固定的,对比时需要忽略文件顺序进行对比. 可以这样设置: 点击菜单下面工具栏按钮: 点击Format旁的三角,选择Sorted,就会按文件的顺序排序比较.忽略了文件内容顺序的 ...
- google C++编程风格指南之头文件的包括顺序
google C++编程风格对头文件的包括顺序作出例如以下指示: (1)为了加强可读性和避免隐含依赖,应使用以下的顺序:C标准库.C++标准库.其他库的头文件.你自己project的头文件.只是这里最 ...
- centos7 中将执行文件python链接为python3后 如何保证 yum 功能不受影响
1. 查看 /usr/bin 中 python 执行文件的 链接情况 2. 设置 python 命令的可执行文件 链接为 python3 3. 此时 , yum 文件中的p ...
随机推荐
- iOS:quartz2D绘图
Quartz-2D:绘图 一.介绍: •Quartz 2D是一个二维图形绘制引擎,支持iOS环境和Mac OS X环境 •Quartz 2D API可以实现许多功能,如基于路径的绘图.透明度.阴影 ...
- [python爬虫] Selenium常见元素定位方法和操作的学习介绍(转载)
转载地址:[python爬虫] Selenium常见元素定位方法和操作的学习介绍 一. 定位元素方法 官网地址:http://selenium-python.readthedocs.org/locat ...
- scrapy-splash抓取动态数据例子四
一.介绍 本例子用scrapy-splash抓取微众圈网站给定关键字抓取咨询信息. 给定关键字:打通:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信息 ...
- LeetCode56:Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- python安装包是出现错误解决
/usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed/limits.h:168:61: fatal error: limits.h: No such file ...
- java 二维码编码解码
做一个小项目的时候写了个二维码编码和解码的小工具,感觉可能用得到,有兴趣的朋友可以看下 再次之前,徐需要用到google的zxing相关的jar包,还有javax相关包 以上为可能用到的jar pac ...
- vector iterator not incrementable For information on how your program can cause an an assertion Failure, see the Visual c + + documentation on asserts
#include <list> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { list<int> sl ...
- 云计算之路-试用Azure:每一次删除都让人如此“心惊”
这篇博文吐槽的是Azure(Virtual Machine)的虚拟机删除功能. 在阿里云中,没有提供直接的虚拟机删除操作,如果不用某台虚拟机,“停止”即可,过期一段时间后会自动释放(这里的释放相当于删 ...
- Android开发--用户定位服务--UserLocation
Android开发--用户定位服务--UserLocation 2013-01-28 08:32:26 我来说两句 作者:BruceZhang 收藏 我要投稿 [java] & ...
- 带 IK 分词器的 Luke 和 搜索应用服务器solr
首先在网上查了一下: Solr Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口.用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索 ...