简体   繁体   中英

Debugging c++ .cpp file using Xcode

I have a solitary .cpp file, and I would very much like to debug it.

Without creating an Xcode project, is there anyway I can debug this using the Xcode debugger?

When I open the file in Xcode to edit and set breakpoints, the program doesn't stop at the break points.

If noone answers you later, maybe this post will help you

I'm not a XCode developer, but I know, that to debug a process you need:

  • a debugger

  • the corresponding (!) code

  • the info, which stores a special debugging info, which gives the debugger an opportunity to map from binary process to your C++ (or other) code

  • and the process, sure :)

this link could help to understand if you have debugging info

http://www.meandmark.com/xcode3debugging.pdf

you can search words "Debug Information" and try to understand, if you have it or not

You can't debug via Xcode without a project because without a project, Xcode works only as a file editor with color highlight and so one.

For debugging you need an executable compiled with debug option, which is produced via a compiler. The easy way (but in my opinion the worse) is to make a Xcode project and put the .cpp file there. There is the hard way too (and probably the best for future reference) that is to learn to do it on the terminal, using for instance the g++ (compiler) and gdb (debugger).

There is a way to do this without floundering through xcode, but unfortunately it is possibly more arcane.

Using cmake ( https://cmake.org/ ) you can write a simple CMakeLists.txt to pull in your cpp file:-

cmake_minimum_required(VERSION 3.10)
project(project)
set(CMAKE_BUILD_TYPE Debug)
add_executable(project code.cpp)

To generate an xcode project type this:-

cmake -G Xcode

this will create project.xcodeproj

You can now type:-

open project.xcodeproj

and xcode will be launched.

If it would help somebody who stumble here when looking for an answer for this topic (similar to what happened to me).

I'm assuming that you do have a project but not able to start debugging. To succeed in doing so, just make sure that prior to clicking the "play" button you have a breakpoint.

To add a breakpoint, just click with the mouse next to the line number, and then you should have a marker similar to the one on line 19 (screen shot below). Now click play, and >>> showtime

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM