如何找出将哪些库链接到现有可执行文件,并适当地重新链接和重新编译代码?

时间:2019-07-19 18:19:16

标签: c++ gcc linker g++ ld

我想使用PinPlay套件中的动态控制流程图工具。在该程序包中,我有一个dcfg-reader.cpp文件,该文件实现了使用DCFG API的示例代码,还可以找到名为dcfg-reader的可执行文件,该文件已预先编译。我想以一种提取我所需信息的方式更改代码。现在,我不知道该代码应如何编译,例如,需要链接哪些库。开发人员刚刚提到要链接两个特定的库(名为libdcfg-pinplay.alibintelzipstream.a)。

在这里您可以看到dcfl-reader.cpp代码的某些部分和主要功能:

#include "dcfg_api.H"
#include "dcfg_trace_api.H"

#include <stdlib.h>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>

using namespace std;
using namespace dcfg_api;
using namespace dcfg_trace_api;

int main(int argc, char* argv[]) {

    cout << "This file is edited!" << endl;
    if (argc < 2)
        usage(argv[0]);

    parse_args(argc, argv);

    if (!dcfg_file)
    {
        cerr << "Missing dcfg file. " << endl;
        usage(argv[0]);
    }
    // Make a new DCFG object.
    DCFG_DATA* dcfg = DCFG_DATA::new_dcfg();


    // Read from file.
    cerr << "Reading DCFG from '" << dcfg_file << "'..." << endl;
    string errMsg;
    if (!dcfg->read(dcfg_file, errMsg)) {
        cerr << "error: " << errMsg << endl;
        return 1;
    }

    // write some summary data from DCFG.
    summarizeDcfg(dcfg);

    // Second argument should be a DCFG-trace file.
    if (edge_file) {
        summarizeTrace(dcfg, edge_file);
    }

    // free memory.
    delete dcfg;

    return 0;
}

请注意:dcfg_api.Hdcfg_trace_api.H文件位于include/目录中。

我使用ldd命令来查找链接到此可执行文件的共享库,这是输出:

 ldd bin/intel64/dcfg-reader 
    linux-vdso.so.1 =>  (0x00007ffec59b3000)
    libbz2.so.1 => /lib/x86_64-linux-gnu/libbz2.so.1 (0x00007f9d7c6bb000)
    libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f9d7c4a1000)
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f9d7c115000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f9d7be0b000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f9d7bbf3000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9d7b829000)
    /lib64/ld-linux-x86-64.so.2 (0x0000556d2b779000)

基于此信息,我使用以下命令重新编译代码:

g++ -std=c++11 -static -I include/ examples/dcfg-reader.cpp -L lib/intel64/ -L /lib/x86_64-linux-gnu/ -L /usr/lib/x86_64-linux-gnu/ -l dcfg-pinplay -l intelzipstream -lm -lbz2 -lz -o out-dcfg-reader

但这会导致大量undefined references,这意味着未链接正确的库。在这里,您可以看到输出的某些部分:

lib/intel64//libdcfg-pinplay.a(DcfgData.o): In function `std::basic_filebuf<char, std::char_traits<char> >::_M_unshift() [clone .part.109]':
DcfgData.cpp:(.text+0x6be): undefined reference to `std::_Filebuf_base::_M_write(char*, long)'
lib/intel64//libdcfg-pinplay.a(DcfgData.o): In function `char* std::priv::__write_integer_backward<unsigned long>(char*, int, unsigned long) [clone .part.570]':
DcfgData.cpp:(.text+0x8a1): undefined reference to `std::priv::__hex_char_table_lo()'
DcfgData.cpp:(.text+0x8f1): undefined reference to `std::priv::__hex_char_table_hi()'
lib/intel64//libdcfg-pinplay.a(DcfgData.o): In function `std::priv::_Slist_base<std::pair<unsigned long const, Dcfg::MtCounts>, std::allocator<std::pair<unsigned long const, Dcfg::MtCounts> > >::_M_erase_after(std::priv::_Slist_node_base*, std::priv::_Slist_node_base*) [clone .isra.520] [clone .constprop.919]':
DcfgData.cpp:(.text+0xfd4): undefined reference to `std::__malloc_alloc::deallocate(void*, unsigned long)'
DcfgData.cpp:(.text+0xfe4): undefined reference to `std::__malloc_alloc::deallocate(void*, unsigned long)'
.
.
.
intel_bzip_ostream.cpp:(.text+0x310): undefined reference to `std::__stl_throw_length_error(char const*)'
lib/intel64//libintelzipstream.a(intel_gzip_ostream.o): In function `intel_gzip_ostreambuf::~intel_gzip_ostreambuf()':
intel_gzip_ostream.cpp:(.text+0x103): undefined reference to `std::__malloc_alloc::deallocate(void*, unsigned long)'
lib/intel64//libintelzipstream.a(intel_gzip_ostream.o): In function `intel_gzip_ostreambuf::intel_gzip_ostreambuf(std::string const&, int)':
intel_gzip_ostream.cpp:(.text+0x21b): undefined reference to `std::__malloc_alloc::allocate(unsigned long)'
intel_gzip_ostream.cpp:(.text+0x240): undefined reference to `std::__stl_throw_length_error(char const*)'
lib/intel64//libintelzipstream.a(intel_bzip_istream.o): In function `intel_bzip_istreambuf::~intel_bzip_istreambuf()':
intel_bzip_istream.cpp:(.text+0x60): undefined reference to `std::__malloc_alloc::deallocate(void*, unsigned long)'
lib/intel64//libintelzipstream.a(intel_bzip_istream.o): In function `intel_bzip_istreambuf::intel_bzip_istreambuf(std::string const&, int)':
intel_bzip_istream.cpp:(.text+0x2bb): undefined reference to `std::__malloc_alloc::allocate(unsigned long)'
intel_bzip_istream.cpp:(.text+0x2e0): undefined reference to `std::__stl_throw_length_error(char const*)'
lib/intel64//libintelzipstream.a(intel_gzip_istream.o): In function `intel_gzip_istreambuf::~intel_gzip_istreambuf()':
intel_gzip_istream.cpp:(.text+0x149): undefined reference to `std::__malloc_alloc::deallocate(void*, unsigned long)'
lib/intel64//libintelzipstream.a(intel_gzip_istream.o): In function `intel_gzip_istreambuf::intel_gzip_istreambuf(std::string const&, int)':
intel_gzip_istream.cpp:(.text+0x24b): undefined reference to `std::__malloc_alloc::allocate(unsigned long)'
intel_gzip_istream.cpp:(.text+0x270): undefined reference to `std::__stl_throw_length_error(char const*)'
collect2: error: ld returned 1 exit status

所以我的问题是,我该如何解决这个问题并正确编译代码。不幸的是,我无法从开发人员那里找到说明如何编译的文档。

谢谢。

0 个答案:

没有答案