我已经阅读了几篇有关堆栈溢出的文章,并在线阅读了有关动态链接的内容。这就是我从所有这些读物中得到的——
动态链接是一种优化技术,用于充分利用系统的虚拟内存。一个进程可以与其他进程共享其页面。例如,libc++ 需要与所有 C++ 程序链接,而不是将可执行文件复制到每个进程,它可以通过共享虚拟页面与许多进程动态链接。
然而,这让我想到了以下问题
*.cpp 文件生成此类文件的具体编译命令是什么?lib/ 目录,其中包含*.a 文件和*.dylib(在mac- OSX) 文件。我如何知道哪些应该像使用常规 *.o 文件一样静态链接,哪些应该动态链接?我假设 *.dylib 文件是动态库。将使用哪个编译器标志来链接到这些?-L 和 -l 标志有什么用?例如,在命令行上指定一个 -lusb 标志是什么意思?如果您觉得这个问题一次问了太多事情,请告诉我。我完全可以将这个问题分成多个问题。我只是一起问他们,因为我觉得一个问题的答案会引出另一个问题。
最佳答案
When a C++ program is compiled. It needs to have references to the C++ library functions and code (say for example the code for the library).
假设我们有一个名为 libdyno.so 的假设共享库。您最终将能够使用 objdump 或 nm 查看它的内部。
objdump --syms libdyno.so
您现在可以在您的系统上使用任何共享库执行此操作。 objdump 在 MAC 上称为 gobjdump 并在 binutils 包中随 brew 一起提供。在 Mac 上试试这个...
gobjdump --syms /usr/lib/libz.dylib
您现在可以看到符号包含在共享对象中。当您链接到共享对象时,您通常会使用类似
g++ -Wall -g -pedantic -ldyno DynoLib_main.cpp -o dyno_main
请注意该命令中的 -ldyno。这是告诉编译器(实际上是链接器 ld)在通常查找它们的任何地方查找名为 libdyno.so 的共享对象文件。一旦找到该对象,它就可以找到所需的符号。没有循环依赖,因为开发人员要求通过指定 -l 标志加载动态库。
How and when would you use a dynamic library? How do you make one? As in what is the specific compiling command that is used to produce such a file from a standard .cpp file
创建一个名为 DynoLib.cpp 的文件
#include "DynoLib.h"
DynamicLib::DynamicLib() {}
int DynamicLib::square(int a) {
return a * a;
}
创建一个名为 DynoLib.h 的文件
#ifndef DYNOLIB_H
#define DYNOLIB_H
class DynamicLib {
public:
DynamicLib();
int square(int a);
};
#endif
按如下方式将它们编译为共享库。这是特定于 Linux 的...
g++ -Wall -g -pedantic -shared -std=c++11 DynoLib.cpp -o libdyno.so
您现在可以使用我之前给出的命令检查此对象,即
objdump --syms libdyno.so
现在创建一个名为 DynoLib_main.cpp 的文件,它将与 libdyno.so 链接,并使用我们刚刚在其中定义的函数。
#include "DynoLib.h"
#include <iostream>
using namespace std;
int main(void) {
DynamicLib *lib = new DynamicLib();
std::cout << "Square " << lib->square(1729) << std::endl;
return 1;
}
编译如下
g++ -Wall -g -pedantic -L. -ldyno DynoLib_main.cpp -o dyno_main
./dyno_main
Square 2989441
您还可以使用 nm 查看主二进制文件。在下文中,我查看是否有任何字符串 square 在其中,即我需要的符号 libdyno.so 在我的二进制文件中以任何方式引用。
nm dyno_runner |grep square
U _ZN10DynamicLib6squareEi
答案是肯定的。大写的 U 表示未定义,但这是我们之前创建的 DynamicLib 类中 square 方法的符号名称。看起来很奇怪的名字是由于名字改编造成的,这是它自己的主题。
How do I know which ones to link to statically as I would with a regular .o file and which ones are supposed to be dynamically linked with?
你不需要知道。您指定要链接的内容并让编译器(和链接器等)完成工作。请注意 -l 标志为库命名,而 -L 告诉它在哪里查找。这里有一篇关于编译器如何找到东西的不错的文章
gcc Linkage option -L: Alternative ways how to specify the path to the dynamic library
或者看看 man ld。
What are the -L and -l flags for? What does it mean to specify for example a -lusb flag on the command line?
请参阅上面的链接。这是来自 man ld..
-L searchdir
Add path searchdir to the list of paths that ld will search for archive libraries and ld control scripts. You may use this option any number of times. The directories are searched in the order in which they are specified on the command line. Directories specified on the command line are searched before the default directories. All -L options apply to all -l options, regardless of the order in which the options appear. -L options do not affect how ld searches for a linker script unless -T option is specified.`
如果您设法到达此处,了解链接器即 ld 会带来红利。它起着重要的作用,并且是大量困惑的根源,因为大多数人开始与编译器打交道并认为 compiler == linker 而这是不正确的。
关于c++ - 动态链接是如何工作的,它的用法以及你如何以及为什么要制作一个 dylib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36560508/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i