Main Menu

Tip jar

If you like CaB and wish to support it, you can use PayPal or KoFi. Thank you, and I hope you continue to enjoy the site - Neil.

Buy Me a Coffee at ko-fi.com

Support CaB

Recent

Welcome to Cook'd and Bomb'd. Please login or sign up.

April 26, 2024, 11:54:09 AM

Login with username, password and session length

C++ Help - Debugging

Started by BlodwynPig, March 20, 2012, 03:09:18 PM

Previous topic - Next topic

BlodwynPig

Does anyone have some C++ experience on here? I am trying to translate some code from C++ to Matlab and have managed to get the basic structure translated across.

However there are some issues with the parameters being called and I would like to display the values used in the C++ code so that I can replicate them in Matlab.

Fucking fiddly.

So I have NEVER used C++ and have been looking at using debuggers such as GDB and Eclipse to try and insert breakpoints at the requisite lines so i can then display the parameter values at that point.

i am getting problems and it looks like its something to do with compilation of the code for debugging. Is this the only way to get access to the internal values of C?

I have also tried editing the .c script and inserting a printf command to display the parameters - but this is not changing the code and I suspect i am not compiling correctly. I used gcc -g filename.c - is that enough?

Any help would be most welcome

cheers

Blumf

First off, is it C or C++ code? You start off talking about C++ then mention .c files and gcc (usually used for C code, as opposed to g++)

Next off, rarely is it just a single .c/cpp file that you compile to produce a full program, usually you need to build it all with the 'make' command, which would explain why the printf changes you've made haven't had any effect. Can you tell us more about how you installed it in the first place?

MojoJojo

I would generally recommend not doing general debugging using GDB or eclipse - they have uses but are a bit fiddly in practice, especially with optimisation in place.
That said you shouldn't need to do anything special to debug with gdb - gdb executable.exe should do it.
Or gdb --args executable.exe argument1 argument2 ... if you need to pass command line parameters to it.

Carefully placed printf's are the way to go.

Compiling (and linking) a basic file would normally be g++ filename.c -o filename.exe
You can skip the -o filename.exe, but the executable will be called a.exe if you do.

If it's a .c file, it's C, not C++. And the .c file is normally called a source file rather than a script, to be a bit pedantic.

BlodwynPig

Thanks Blumf.

Its a tortuous route. So, yes it is C++, but I will have to double check.

The code was downloaded as a tarball, unpacked and compiled with make. There are a number of files unpacked, of which this one is just one. it comes with a header file and has a number of dependencies (gnu scientific library functions). Essentially you just need gsl installed on your machine for these to be utilised.

I might try the g++ option as I saw that in a technical thread somewhere - completely new to the world of C, but having to learn this rapidly just to translate the code into more familiar Matlab.

BlodwynPig

ok the make instruction now seems to point to the updated c file as it gives me errors about the line I entered

MetroIG.c:329: warning: format '%d' expects type 'int', but argument 2 has type 'double'

MojoJojo

%d prints integers. For a double %g is probably what you want, although it depends a bit on what sort of values you expect the number to have.

BlodwynPig

ok, the %g removed the warning, but then running the compiled command failed to do anything

here is the code

http://pastebin.com/JMxaQwk0

Blumf

Quote from: BlodwynPig on March 20, 2012, 03:25:17 PM
The code was downloaded as a tarball, unpacked and compiled with make. There are a number of files unpacked, of which this one is just one. it comes with a header file and has a number of dependencies (gnu scientific library functions). Essentially you just need gsl installed on your machine for these to be utilised.

Sounds pretty normal, in which case just use make to rebuild the code when you've made a change. Building a single source file (C, C++, whatever...) won't touch the final executable.[nb]Generally programs are built as follows:
source files(*.c) --[gcc/g++]--> object files (*.o) --[link]--> executable file[/nb]

The make command handles all that for you, so change the source file then run make again.

BlodwynPig

1. Changed source file metroig.c (amended one letter in a printf function so I could see if it works)
2. Ran make in the source file folder
3. Ran MetroIG -args

Output was still the old file!

Blumf

Quote from: BlodwynPig on March 20, 2012, 03:33:58 PM
ok, the %g removed the warning, but then running the compiled command failed to do anything

Are you sure you're running the changed executable and not the installed one? You, have to be explicit i.e.:

./src/thing
(or where ever the built executable ends up after make)

as opposed to just

thing

Which might default to whatever the installed, unchanged version is.

which thing

Should tell you what's being run.

BlodwynPig

You beautiful bastard! Of course...that worked...however!! the bloody bit of script I added did not spit anything out - the printf. I will check on that...

BlodwynPig

Tried it in another function and that worked - the function I have must not be called.

Thanks for your help Blumf!

Mary is not amused

Was it this you added?: printf("Alpha = %d Beta = %d n = %d\n",&dAlpha, &dBeta, &dN);

Because as well as changing the %d you'd need to remove the ampersands (although that would make the original warning non-sensical too).  Moot now.

MojoJojo

I noticed that too Mary - but assumed that couldn't be the line the warning was about, because why would the compiler be calling pointers doubles???

Mary is not amused

Sorry, I edit like a ninja!

I agree with you, though.

BlodwynPig

Yes, i removed the ampersands (only added due to another forum). Works now, although the original printf does not output, suggesting that function is not called at all - so have to check that...i added it in the Bessel function and got iterative output for my parameters, which confirms what I am looking for. Cheers

BlodwynPig

  status = bessel(&dRet,n, dAlpha,dBeta);
  if(status == FALSE){
    dRet = sd(n, dAlpha,dBeta);
  }


BlodwynPig

Fucking beauties - problem with the output is now resolved thanks to you guys (i.e. I have identified the mistake in my translated code)!