Using LLDB API Segment Fault

Hi everyone, I want to build a debugger frontend making use of LLDB’s API (on ubuntu linux 64bit). Since there’s not enough document/tutorial on using LLDB API, I turned to the “Python reference” and made a little program:

`#include <iostream>`
`#include <lldb/API/LLDB.h>`
`#include <lldb/API/SBDebugger.h>`
`#include <lldb/API/SBTarget.h>`
`using namespace std;`
`using namespace lldb;`

`int main() {`
`cout << "Hello LLDB!" << endl;`
`SBDebugger debugger = SBDebugger::Create();`
`SBTarget target = debugger.CreateTargetWithFileAndArch("/home/szm/target", LLDB_ARCH_DEFAULT);`
`return 0;`

I compile and link the program using command: g++ -std=c++0x -o lldbwrapper lldbwrapper.cpp -I /usr/lib/llvm-3.5/include -llldb. It compiled without error, but when I run the program it printed “Helo, LLDB!” and then printed “Segment fault”.

What’s more, if I use the same code in Qt Creator with a plain c++ project (using qmake, with C++11 enabled), the program just run normally.

I inspected Qt’s compile command is:

`g++ -c -pipe -g -std=c++0x -Wall -W -fPIE -I/opt/Qt5.3.1/5.3/gcc_64/mkspecs/linux-g++ -I../lldbtest -I/usr/lib/llvm-3.5/include -I. -o main.o ../lldbtest/main.cpp`
`g++ -Wl,-rpath,/opt/Qt5.3.1/5.3/gcc_64 -o lldbtest main.o -I /usr/lib/llvm-3.5/include -llldb`

But I didnt see any difference, what’s the cause of the segment fault only in command line?


Song Ziming

I figured out myself, just add SBDebugger::Initialize() at the first line of main function.

Besides, the code in Qt Creator and in command line actually behaved the same. But Qt Creator redirected the segment fault message to another window so I didn’t noticed.