How to debug a Gradle cpp application with GDB?


(Ernani Rordrigues de SThiago) #1


I recently got started with gradle for c++ and seems great. However, I yet wasn’t able to debug my app with GDB. So, here is my simple app:

apply plugin: 'cpp'

model {
    components {
    	hello(NativeLibrarySpec) {
            binaries.all {
                cppCompiler.define "HELLO_EXPORT"
        main(NativeExecutableSpec) {
            binaries.all {
                lib library: "hello"
                cppCompiler.args "-g"

Basically, I got a main at src/main/cpp and a module at src/hello/cpp. I’m simply trying to compile this project with the argument -g in order to be able to debug it with GDB. So I just installed the project with gradle clean build installMainExecutable and it was succesfull, I can actually run my application with ./build/install/main/main, however this main is a script, so I can’t use gdb on it, so I tried to run gdb with ./build/install/main/lib/main and it loads succesfully, however when I actually run the program, gdb says it could not find the shared libraries: error while loading shared libraries:

So, what am I getting wrong? Is there a way of debugging with that main shell script? Or should I just keep looking on how to load the shared libraries “by hand” with GDB?

Best Regards, Ernani

(Chris Doré) #2

If you look at the script generated by the install task, one of the key things it does it set the library path environment variables. If you set LD_LIBRARY_PATH (or DYLD_LIBRARY_PATH depending on your OS) when running gdb, it should find the missing libs.

For example, on Linux:

LD_LIBRARY_PATH=build/install/main/lib gdb build/install/main/lib/main

(Ernani Rordrigues de SThiago) #3

Thanks a lot @Chris_Dore, that solved my issue.