简体   繁体   English

c ++ Pin共享库未定义符号

[英]c++ Pin shared library undefined symbol

I am using the Intel Pin tool to generate a shared object file from a C source file and a C++ pintool. 我正在使用Intel Pin工具从C源文件和C ++ pintool生成共享对象文件。 I have used the following g++ commands to generate my shared object file. 我使用以下g ++命令生成共享对象文件。 Apologies in advance as the commands are very big due to the Pin libraries and options. 由于Pin库和选项的缘故,命令非常大,因此请您提前道歉。

g++ -DBIGARRAY_MULTIPLIER=1 -Wall -Werror -Wno-unknown-pragmas -D__PIN__=1 -DPIN_CRT=1  -fno-stack-protector -fno-exceptions -funwind-tables -fasynchronous-unwind-tables -fno-rtti -DTARGET_IA32E -DHOST_IA32E -fPIC -DTARGET_LINUX -fabi-version=2  -I/home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/source/include/pin -I/home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/source/include/pin/gen -isystem /home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/extras/stlport/include -isystem /home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/extras/libstdc++/include -isystem /home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/extras/crt/include -isystem /home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/extras/crt/include/arch-x86_64 -isystem /home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/extras/crt/include/kernel/uapi -isystem /home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/extras/crt/include/kernel/uapi/asm-x86 -I/home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/extras/components/include -I/home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/extras/xed-intel64/include/xed -Iinc -I/home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/source/tools/InstLib -O0   -c -o obj-intel64/testcpp.o testcpp.cpp

g++ -DBIGARRAY_MULTIPLIER=1 -Wall -Werror -Wno-unknown-pragmas -D__PIN__=1 -DPIN_CRT=1  -fno-stack-protector -fno-exceptions -funwind-tables -fasynchronous-unwind-tables -fno-rtti -DTARGET_IA32E -DHOST_IA32E -fPIC -DTARGET_LINUX -fabi-version=2  -I/home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/source/include/pin -I/home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/source/include/pin/gen -isystem /home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/extras/stlport/include -isystem /home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/extras/libstdc++/include -isystem /home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/extras/crt/include -isystem /home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/extras/crt/include/arch-x86_64 -isystem /home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/extras/crt/include/kernel/uapi -isystem /home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/extras/crt/include/kernel/uapi/asm-x86 -I/home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/extras/components/include -I/home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/extras/xed-intel64/include/xed -Iinc -I/home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/source/tools/InstLib -O0   -c -o obj-intel64/test.o test.c

g++ -shared -Wl,-z,defs -Wl,--hash-style=sysv /home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/intel64/runtime/pincrt/crtbeginS.o -Wl,-Bsymbolic -Wl,--version-script=/home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/source/include/pin/pintool.ver -fabi-version=2   obj-intel64/testcpp.so -o obj-intel64/testcpp.o obj-intel64/test.o  -L/home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/intel64/runtime/pincrt -L/home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/intel64/lib -L/home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/intel64/lib-ext -L/home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/extras/xed-intel64/lib -lpin -lxed /home/rohit/github_repos/uli/src/pin/pin-3.2-81205-gcc-linux/intel64/runtime/pincrt/crtendS.o -lpin3dwarf  -ldl-dynamic -nostdlib -lstlport-dynamic -lm-dynamic -lc-dynamic -lunwind-dynamic

testcpp.cpp is my C++ pintool and test.c is my C source file. testcpp.cpp是我的C ++ pintool,而test.c是我的C源文件。 The folder inc contains the header file test.h which is included in both of them. 文件夹inc包含两个文件中的头文件test.h。

This is my C++ file - 这是我的C ++文件-

#include "pin.H"
#include <iostream>
#include <fstream>

extern "C" {
#include <test.h>
}

using namespace std;

KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", "o", "test.out","This pin tool simulates ULI");

FILE * op;

VOID Instruction(INS ins, void *v) {
}

VOID Fini(INT32 code, VOID *v) {
    fprintf(op,"Ended from c++\n");
    fprintf(op,"%d\n",cfunc(11,2));
    fclose(op);
}

INT32 Usage() {
    PIN_ERROR("This Pintool failed\n" + KNOB_BASE::StringKnobSummary() + "\n");
    return -1;
}

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

    if (PIN_Init(argc, argv)) 
        return Usage();

    op = fopen("test.out", "w");

    PIN_InitSymbols();
    INS_AddInstrumentFunction(Instruction, 0);
    PIN_AddFiniFunction(Fini, 0);
    PIN_StartProgram();

    return 0;
}

This is my C file- 这是我的C文件-

#include <stdio.h>
#include <test.h>

int cfunc(int a, int b)
{
    return(a+b);
}

void cfunc2()
{
    printf("Hello from C %d\n",cfunc(3,5));
}

This is my test.h file- 这是我的test.h文件-

int cfunc(int, int);
void cfunc2(void);

The rest of the options are pin specific and I simply used them from the sample programs and feel that they don't need modifications (I hope so!) 其余选项是特定于引脚的,我只是在示例程序中使用了它们,并认为它们不需要修改(我希望如此!)

However when I generate my shared object file I get this error- 但是,当我生成共享对象文件时,出现此错误-

obj-intel64/testcpp.o: In function `Fini(int, void*)':
testcpp.cpp:(.text+0x41): undefined reference to `cfunc2'
collect2: error: ld returned 1 exit status

cfunc2 is the function I am trying to call. cfunc2是我尝试调用的函数。 I do not understand where I am going wrong. 我不明白我要去哪里错了。 I am including the right options and arguments, but somehow this symbol is undefined. 我提供了正确的选项和参数,但是以某种方式未定义此符号。 Any suggestions? 有什么建议么?

You use g++ to compile the test.c file. 您使用g++编译test.c文件。 The g++ compiler driver invokes the C++ compiler for .c files, so your functions will have C++ linkage, not C linkage. g++编译器驱动程序为.c文件调用C ++编译器,因此您的函数将具有C ++链接,而不是C链接。 There are several ways to address thos: 有几种解决方法:

  • Compile test.c with gcc instead of g++ . gcc而不是g++编译test.c
  • Drop the extern "C" from the C++ source file, so that C++ linkage is used everywhere. 从C ++源文件中删除extern "C" ,以便在所有地方都使用C ++链接。
  • Put the extern "C" into the header file, so that applies to the definition in test.c , too. extern "C"放在头文件中,这样也适用于test.c的定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 混合C / C ++代码会在共享库中产生“未定义符号” - Mixing C/C++ code yields “undefined symbol” with shared library C ++共享库中基类的未定义符号错误 - Undefined symbol error for base class in C++ shared library 尝试在 python session 中导入用 c++ 编写的共享库时出现“未定义符号”错误 - getting `undefined symbol` errors when trying to import shared library written in c++ in python session C ++ Linux错误正在加载共享库`undefined symbol:pthread_create` - C++ Linux Error Loading Shared Library `undefined symbol: pthread_create` 在Python中加载C共享库期间发生OSError(未定义符号:checkedCalloc) - OSError during loading of C shared library in Python (undefined symbol:checkedCalloc) C ++未定义的符号 - C++ undefined symbol Python/C++ 扩展,链接库时出现未定义符号错误 - Python/C++ Extension, Undefined symbol error when linking a library 使用共享库时对函数的 C++ 代码未定义引用 - C++ code undefined reference to function when using shared library C++ 共享库未定义对 `FooClass::SayHello()&#39; 的引用 - C++ shared library undefined reference to `FooClass::SayHello()' 加载共享库时未定义的符号 - Undefined symbol when loading a shared library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM