I've been switching a project to CMake and VSCode on my Mac, and encountered a baffling problem: when I enable LTO/IPO, VSCode's C++ debugger doesn't highlight the execution line when breaking. This appears to affect any build target for which I enable IPO.
Here's a minimum example demonstrating the problem:
[main.cpp]
#include <iostream>
int main()
{
std::cout << "This is some text!" << std::endl;
__asm__("int $3"); // Alternatively, use a breakpoint
return 0;
}
[CMakeLists.txt]
cmake_minimum_required(VERSION 3.23.0)
project(TestLTODebugging)
include(CheckIPOSupported)
check_ipo_supported()
add_executable(example main.cpp)
set_target_properties(example PROPERTIES
INTERPROCEDURAL_OPTIMIZATION TRUE
)
I'm using Clang and Ninja, using quick-debugging via VSCode's CMake Tools extension. When run, the app appears to pause as expected, but the line is not highlighted:
And if all I do is turn off IPO/LTO, the issue goes away:
Anyone know where to look for the problem? I'm new to CMake and using VSCode for C++, but this seems super basic and I can't find anything online about this problem. I've had no problems with this when generating an Xcode project with the same source.
While I'm primarily going to be debugging without optimizations, the project I'm working on requires working with optimizations enabled a lot of the time, and I want the debugger to work. I appreciate any help or advice.
EDIT: I've confirmed that this issue does not occur on my PC, where I also tested with Ninja, Clang, and the VSCode debugger. So the problem is either with my Mac environment or is Mac-specific?
EDIT 2: I've now confirmed this affects me with both Ninja and Make files on my Mac. Additionally, both Ninja.build and compile_commands.json (when using Make) show that the only difference between enabling and disabling IPO is the addition of the -flto=thin flag. The -g flag is never removed, and using nm in Terminal shows that the debug symbols are still being generated. I'm now more confident that this is a bug with VS Code or CMake Tools.
I faced a similar problem -- in my case I source annotations for Instruments were missing.
From here:
Note
On Darwin, when using -flto along with -g and compiling and linking in separate steps, you also need to pass -Wl,-object_path_lto,<lto-filename>.o at the linking step to instruct the ld64 linker not to delete the temporary object file generated during Link Time Optimization (this flag is automatically passed to the linker by Clang if compilation and linking are done in a single step). This allows debugging the executable as well as generating the .dSYM bundle using dsymutil(1).
Additional information here.
TL;DR: just add -Wl,-object_path_lto,lto.o to your linker options -- e.g. for CMake: add_link_options(-Wl,-object_path_lto,lto.o).
I'm attempting to explore MariaDB's source code and thought that the easiest way would be to use a C++ IDE. I used the Bash script debian/autobake-deb.sh bundled in MariaDB to compile the project, then opened it with CLion. Unfortunately it looks like a lot of code is not recognized (even though CMake is bundled with CLion) which prevents me from using autocomplete, function references and such in a lot of places.
For instance when I open libmysqld.c :
There are errors almost every line.
I'm a beginner in C/C++ so I'm not sure what I would have to configure or install to make CLion recognize the symbols better.
edit : Updated screenshot with status bar
I have a large C++ project, different parts of which are compiled in different ways (bjam/make). I was having trouble getting it to build from within Eclipse. Could I build the code externally (with g++ -g), and then just run the debugger from within Eclipse?
I figured it out. Should've looked earlier at this: http://wiki.eclipse.org/CDT/User/FAQ#Debugging_C.2FC.2B.2B_Projects
I first added my project to eclipse. Then I followed the instructions above to set up a debug configuration. And somewhat surprisingly eclipse is able to figure out that the binary I want to run refers to the project that I have open, and so I can actually set breakpoints, step through the code while eclipse shows me which line in which file I'm at, etc. So its exactly as if I built the project from within eclipse. Its impressive :)
I am trying to open an existing C++ open-source library in Xcode to publish it with my own modification/additions. The library is Tesseract-OCR, which does not include a .xcodeproj file.
Since Xcode can function as an IDE, is it possible to open a bunch of files as a single project in Xcode? Is there an easy way to produce an Xcode project?
There are several ways you could do it, depending on the level of IDE integration you want. There's no direct way of importing a Makefile-based project into Xcode. You can create a project that builds via the Makefile, but you wouldn't get many of the benefits of using an IDE, since the editor features such as word completion rely on Xcode being able to parse the files in the project. You will be able to use the debugger though. To do this, create a new project and add a custom target with a script build phase that just calls down to Makefile.
If however the project you're building compiles very easily, ie without requiring a lot of macros to be set up, include paths, etc, then it may be simple to just create an empty project and merely add all source files to it. I've used this method extensively for building boost libraries. If this is a configure && make type project then you will probably have to run the configure step first, and ensure any top level config.h files are included in the project.
If the project has a complex makefile then it is likely to be an involved task to create a useful Xcode project
I realise you asked explicitly for Xcode, but in case you were actually trying to solve the problem of "I have existing C++ code which builds and runs fine from the command line, and I'd like to code and debug it in an IDE, what should I do?" my firm recommendation would be to avoid Xcode and go for Eclipse.
The reason is that as far as I can tell, Xcode has no way of ingesting the command line build environment and effectively requires you to recreate the make process inside Xcode from scratch. Fine for tiny projects, but anything with more than a few source files and it quickly becomes painful. Whereas in Eclipse everything is built around Makefiles. So in my case I got to the "step through code with working code completion" in Eclipse a lot quicker vs. never in Xcode. This of course could be because I'm an Xcode noob, but my 2c.
To create an Xcode project from an existing cmake project, you can run cmake -G Xcode. It produces some folders and files apart from the project file, so it might be better to create a folder for it first. For example:
mkdir -p build/xcode
cd build/xcode
cmake -G Xcode ../..
Xcode is a useable IDE for library creation.
Of course a good first step is to see if the one source code will build on its own with configure scripts that are included.
If not, it becomes a question of how many libraries you need to link in.
There are resources online (or at least there used to be) for using Xcode (or perhaps it's forerunner Product builder) for porting Unix projects to Mac.
Good tutorial at: http://www.macresearch.org/tutorial-introducing-xcode-30-organizer
Another good reference is Darwin Ports.
As for doing this on your own. You can build c++ based libraries in XCode. People do that every day. You can even use one of the Xcode templates to get you started.
However, library dev requires more experience with Xcode then say a simple Cocoa "Hello World" app.
The remaining questions will be assuring that the source code's dependencies are already built into the Mac's SDK. (Don't hold your breath for linking to MFC)
It's a general question... So it's a general answer.
In Xcode8,there is "Xcode->file->add files to...",then choose your files.If you want to add several files at a time,press "Cmd" when you are choosing.
I'm having trouble debugging a C++ program in Eclipse (the latest RC of Helios, updated with latest CDT from within itself) on OSX.
The program is very simple (esentially Lesson 2 from NeHe's OpenGL tutorials), consisting of one cpp file and, using OpenGL and Cocoa frameworks, and linking with libSDL.a and libSDLmain.a.
The structure of the project is very simple: the source file(s) are in a subdirectory of the project called src/ and the executable is built to the project's root directory.
The problem is that whenever I try to add breakpoints and debug it, the breakpoints seem to get hit perfectly but no source is displayed - instead I just get a "No source available for main()" error in the code window.
The compiler flags have optimisations set to none, and both the compiler and linker have the debug symbols flag set (-g).
The debugging setting in Eclipse is set to "Standard spawn progess" and the debugger is set to "gdb".
Now the strangest thing is that if I try to debug the exact same executable - ie. the exact same one that was built by Eclipse - using gdb from the Terminal (shell) then everything works fine. Breakpoints are hit, source code is displayed, no problems at all.
I've made sure that both Eclipse and the shell are using the same gdb executable, and they are (it's /usr/bin/gdb).
Now I may be wrong, but this all suggests to me that there can't be a problem with the compiler and linker flags (because the same executable is debuggable from the shell), so presumably the problem must be with how gdb is being invoked from within Eclipse? Perhaps when run from Eclipse gdb is picking up different config files or something than when it's run from the shell? (Anyone know?)
I'd really appreciate any help with this because it's slowly driving me loopy!
Please let me know if there are any other details that would be useful - exact version numbers of Eclipse/cdt/gdb, exact linker/compiler command lines, etc. - and I'll very gladly update this post with them.
Many thanks in advance,
thoughton.
--- edited # "14 hours ago" ---
I tried the "add filesystem path" (with "search sub-folders") option, but that didn't work. I also tried creating a new completely flat project, but that didn't work either.
I even tried getting a Galileo release (eclipse-SDK-3.5.2RC4 with CDT update), but that made no difference (apart from gdb being slower to launch).
And here's something else strange I noticed: once I get the "No source available" message, if I then switch Eclipse's Console to display the "gdb" console, and also turn on "Verbose console mode" so I can communicate it, I can then issue "l" and "bt" commands and have them work succesfully, showing the correct source and stack where my breakpoint was hit. Which, correct me if I'm wrong, must mean that the information is there and gdb is being invoked correctly - so why will Eclipse not see this information?
I'm getting close to giving up on Eclipse to be honest... I came to it with such high hopes, too.
Any additional help or thoughts would be hugely appreciated.
t.
This thread suggests:
-g -O0
for debug flags to be set for Eclipse CDT compilation.
Sometime, it is simple a problem of rebuilding completely the application (like here)
See also this thread describing a similar situation:
I have noticed that sometimes in Eclipse I have to go and specifically add the path to my source files using the "add filesystem path" (with "search sub-folders") in the Debug Dialog (even when they are in the same project I am debugging), but I have not noticed a pattern to when I have to do this. But it may be worth a try.
I found the answer! And it's embarrassingly simple.
The problem was that I was using the Release version of SDL instead of the Debug version! (I had 'libsdl' from MacPorts whereas I should have had 'libsdl-devel'.)
So my generic answer is: make sure the libs you're linking against were compiled with debug flags set too, it's not always enough to just make sure your own code has them set.
Here is another reason for this problem. My configuration used -g3 as the option to gcc. Changing it to -g solved the problem. There seems to be some incompatibility between gcc and gdb. I checked that gdb was the latest revision (using apt-get).
I would like to add a little new blood to this old thread.
I encountered this problem when I tried to compile and debug a gnu arm project.
I solved the problem by modifying the Makefile:
adding "-g -O0" at the end of this line "CFLAGS += -Wall -Werror -O3"
Go to project Properties, C/C++ Build -> Settings. On the first tab (Tool Settings) under Cross GCC Compiler click Debugging and set Debug Level to Maximum (-g3)
I had this issue when I compiled the latest gcc, but did not update to the most recent gdb. After the update, it worked properly.
Thought to mention, that in case you are using cmake to build the project, one approach to the solution will be to add the "debug flag" to the cmake command, i.e. -
$ cmake /path/to/main/cmake_file -DCMAKE_BUILD_TYPE=Debug
For anyone else who may experience this issue,
I installed the linuxtools/valgrind plugin last night to do some memory profiling, and it seems that this broke the normal gdb. when i removed the linuxtools plugins everything started working as normal again.
So you might like to try that.
I had a similar problem. I was using CFLAGS=-Wall -O2 -fPIC -DPIC -lm -lasound and never had problem to compile it but when I tried to debbug it on Eclipse IDE I get this error: No source available for "main() at 0x401080" then I added -g to this line and it worked well:
CFLAGS=-g -Wall -O2 -fPIC -DPIC -lm -lasound
This issue depends on how gdb is being invoked. I found I needed to manually specify the source file locations when I got that error. Even though I'd already configured that under project properties. After doing so, Eclipse no longer had a problem supplying the appropriate source.
Using the release versus debug version of a library may be your specific problem (if you were building a library from source then debugging it). If someone is using a precompiled library, they'd never be able to set breakpoints within it and so that fix wouldn't apply to them.
Encountered the same problem once. You just have to go to your Project Properties, by hitting ALT+ENTR or right click project and scroll down at the bottom and you will find Properties. Expand C/C++ build on the left. Then click on settings. Once you open the settings, then click on tool settings. In the MCU GCC Compiler, there is a debugging option. Click on debugging and add
-g -O0
in the Other debugging flags. Trying debugging the project now.
I just faced with this issue and after take some time to find it out, I realize that the Arguments and Main tab in Debug Configurations dialog are conflicting each other.
Make sure that C/C++ Application and Programs Arguments point to the same binary file.
Seems like this message can have plenty of reasons to show.
For me (in the context of micro controller debugging) it was the link-time optimization. With -flto it broke; removing -flto from the "Other Options" field fixed this for me.
In Eclipse Neon (4.6) see Project -> Properties -> C/C++ Build -> Settings -> Tool Settings -> C Compiler -> Miscellaneous -> Other options.
Add this to your CMakeLists.txt
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS "-g")
set(CMAKE_C_FLAGS "-g")