[英]OS X 10.9: cannot debug c++ dynamic library made with g++
I've read multiple posts here relating to dynamic libraries on os x and debugging with gdb. 我在这里阅读了多篇有关os x上的动态库和gdb调试的文章。 But I still can't figure out why I can't debug a simple test case.
但是我仍然不知道为什么我不能调试一个简单的测试用例。
The main issue is that when I start up GDB it never loads any shared libraries. 主要问题是,当我启动GDB时,它永远不会加载任何共享库。
Update: I've tried this with GDB from macports, from homebrew, and built from source and the behavior is the same. 更新:我已经从Macports,自制软件,从源代码构建GDB进行了尝试,其行为是相同的。
I have a class that I compile into a library. 我有一个可以编译为库的类。
Test.hpp Test.hpp
class Test {
public:
void set(int i);
void out() const;
private:
int i;
};
Test.cpp TEST.CPP
#include "Test.hpp"
#include <iostream>
void Test::set(int ii) { i = ii; }
void Test::out() const {
auto j = i * 100;
std::cout << i << ", " << j << "\n";
++j;
std::cout << i << ", " << j << "\n";
}
I compile it and create a library with g++. 我编译它并使用g ++创建一个库。 Note: the behavior is the same with macports gcc and the gcc from xcode.
注意:macports gcc和xcode中的gcc的行为相同。
/opt/local/bin/g++-mp-4.8 -O0 -g -ggdb -Wall -c -std=c++11 -o Test.o Test.cpp
/opt/local/bin/g++-mp-4.8 -dynamiclib -o libTest.dylib Test.o
Then I test it with this simple main 然后我用这个简单的主测试
#include "Test.hpp"
int main() {
Test t;
auto x = 4;
t.set(x);
t.out();
return 0;
}
This is compiled and linked with 这被编译并与
/opt/local/bin/g++-mp-4.8 -O0 -g -ggdb -Wall -c -std=c++11 -o main.o main.cpp
/opt/local/bin/g++-mp-4.8 -L . -o testing main.o -lTest
Everything compiles and runs as expected. 一切都会编译并按预期运行。 But when I try to debug this with gdb (installed from macports, or installed from source, the behavior is the same), I have problems.
但是,当我尝试使用gdb调试(从macports安装或从源安装,行为相同)时,出现了问题。
As I step through main, if I call info sharedlibrary
it always says "No shared libraries loaded at this time.", so it apparently never loads libTest.dylib
. 当我逐步了解main时,如果我调用
info sharedlibrary
它总是说“此时未加载共享库。”,因此它显然永远不会加载libTest.dylib
。 Therefore, I can't step into any of the Test
member functions or create breakpoints anywhere in libTest.dylib
. 因此,我无法进入任何
Test
成员函数,也不能在libTest.dylib
任何位置创建断点。
Indeed ggdb installed from macports for some reason does not respect the DYLD_LIBRARY_PATH. 确实由于某些原因从macports安装的ggdb不遵守DYLD_LIBRARY_PATH。 However, if you "patch" your executable with the correct paths for the .dylibs you should be able to debug with ggdb.
但是,如果使用.dylibs的正确路径“修补”可执行文件,则应该可以使用ggdb进行调试。 Take a look at this question and especially the answer by Akos Cz.
看一下这个问题 ,尤其是Akos Cz的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.