How to debug dylib with Xcode?

流过昼夜 提交于 2019-12-03 03:27:00

I could find a way to debug dynamic library in Xcode.

Build

  1. Make a library, I'll call this arith library. Debug build to make libarith.dylib.
  2. Make a project to use the library, I'll call this usearith.
  3. For userarith, Project->Add To Project, and add the arith library.
  4. Open Project info, and open the Build tab.
  5. Go to Search Paths/Library Search Paths, drag and drop the arith library. You should remove the library name as you need only specify the path. Specify the header directory with 'Header Search Paths'.
  6. Go to Linking, set Other Linker Flags, add -larith

Running

Now, you should be able to link the execution binary to the library. For running, you need to copy the dynamic library to the directory where the execution binary is located.

Debugging

You need to set the breakpoints both arith/usearith. You can run debugger in arith and use the step into to debug the code in a arith project.

I faced the same problem and no one of the previous answer worked for my case so I share my solution (for Xcode):

If you need to debug a c/c++ dylib which is loaded by an external (executable) program:

  1. First be sure that your dylib is build with the same architecture as your external program.
  2. Then Go to --> Product —>Scheme—>Edit scheme
  3. Got to Tab Run(Debug) and check "Debug Executable" , then select into the dropdown button your external program as executable. Then check "Launch Automatically"
  4. Additionally if you program needs extra argument you can add it into the "Arguments" tab.
  5. Finally you set some breakpoints to your c source file and finally click run.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!