How do you debug C code in visual studio code - c

If I am to compile my code in the terminal, I would do:
cc -std=c99 -Wall -Werror ....
If I'm to run my code in the terminal I would do:
./testprogram text.txt 1000 1000
The numbers and the text file is significant to the program.
Hopefully that gives some insight into the version of c I'm using and how to execute my program. Now: how do you debug this with visual studio code? I have installed the c/c++ extension.
Every time I try to start debug it asks me to chose between two environments:
c++ (GDB/LLDB)
c++ (windows).
My first problem is that non of those two options are just plain c, but maybe I just don't know better and there isn't much difference. Never the less, I gamble on one of the environment, usually the first one, then I'm asked to pick between:
two gcc-9 build and debug active file,
one gcc build and debug active file,
and defult configuration.
I usually pick the defult config one and that leads me to the launch.json page. I put in my program directory: "${workspaceFolder}/testprogram.c" and error.. :
Unable to start debugging. Launch options string provided by the project system is invalid. Unable to determine path to debugger. Please specify the "MIDebuggerPath" option.
Nothing I've tried so far or any googling have helped so hopefully someone with a lot of experience would be able to help out here. Thanks in advance.

Related

Using Visual Studio 2008 with Code Warrior compiler

New to the site. I am hoping you can help me as I've been beating my head against the wall on this one for a month now.
This is for work, and we are kind of proprietary but I will try to share as much information as possible. We have MPC5674F microcontroller on our main development board. To write code before, we used Microsoft Visual Studio 2008 along with a makefile and gcc compiler to create a .s19 file that is then flashed onto the board. Before, Visual Studio would not recognize make command. I went into C:\MinGW folder and found 3 different make.exe type files. There is gnatmake.exe, mingw32-make.exe and regular make.exe in C:\MinGW\msys\1.0\bin. I also found a regular make.exe in an NXP folder from the S32Design Studio. All of those make commands give me different result. If I change one that is by MinGW, then it gets stuck in the makefile saying The system cannot find the path specified to an echo.exe command, even though I can once again go into command window and type echo and it outputs ECHO is on. If I use just make.exe from either NXP of the other folder from MinGW, it compiles without errors but there is no file output, or even the process of how it compiled. In our makefile we specify to output each file that it compiles and nothing happens. If I use gnatmake.exe It gets stuck at command gcc -c -Ietc\ -I- -x ada etc\makefile where it says there is illegal character and target_name is an Ada 2020 feature. Not sure what any of those mean.
I managed to somehow previously get this to compile before, but since then the PC (Windows 10) had to be wiped and reinstalled. Guys that have set this up are long since retired (before I started) so I am hoping you guys can help me out.
Okay so I managed to get it to work. How, I am not entirely sure. I did wind up editing my commands for linux inside the makefile to have quotes around them because Windows kept removing slashes from the path written in the makefile. Also I provided a path to the Unix utilities and the make.exe to C:\MinGW\msys\1.0\bin instead of C:\MinGW\bin. I also had to add the same path to the $Path variable in Environmental Variable The code compiles now and seems to work after flashing it onto our system. My hope now is the instructions I wrote for anyone else to do this will work and I didn't forget something.

Creating an executable file at Visual Studio Code

I am relatively new in programming using the C-language. So to lay it all out, I am still kind of clueless of what is happening. Now, may I ask why I can't seem to create an exe file and run the code?
Actually, the first time I tried executing it, it worked. However, to make sure that it is working, I repeated it and then it didn't work anymore.
To make things clearer:
I use Mac
Mac OS Big Sur
In this particular case, gcc is the compiler
I also tried using Code::Blocks also prior to this; however, I have similar issues -- after I built and ran my first "hello world" program, it didn't run the same program anymore after it. I also tried reinstalling it, but it still has the same issues.
I'm really frustrated that this is happening because I cannot move on to the next lessons in my class. :(
Thank you very much! I hope someone would help a newbie here. :))
Gcc by default names executables a.out. So you can check if that file has been created by running ls a.out and looking through the results.
If it has been made, you can run it from the command line with ./a.out.
If you want to name the executables something else, then when calling gcc, add the flag -o followed by the name you want. E.g. if you want filename.c to compile to filename.exe, then you would write gcc -o filename.exe filename.c. Then you can run it from the command line with ./filename.exe. I think if you give it the .exe file extension you should also be able to run it by clicking it in the Windows file explorer.

Monotouch LLVM compiler doesn't work

Hello all I hope I can find a solution for this,I have my project in monotouch and I have configured my AdHoc profile for test on devices, I am able to generate the apps using the standard compiler, but when I want to use the LLVM compiler this compilation process literally never ends .. I left my mac turned on all the night .. I even tried changing the linker options (I know is not recommended). In short I tried with link sdk assemblies only and with don't link. Still the process never ends. If someone is experiencing the she problems and could help me please. Thanks.
Never seen this - but that's definitively a bug and should be reported to Xamarin.
Please trying adding -v -v -v to your Additional mtouch arguments (in the project's options) so the verbosity will be at the maximum.
The rebuild and switch the the "Build Output" (it's in the Error List pad inside MonoDevelop). Once it seems stopped copy-paste the log and include it in the bug report. That should give enough information to diagnose the issue.

Cygwin Gcc - Access

I'm currently on Windows Vista Basic.
I have recently started on Eclipse, and for that I was required to install cygwin.
After that I made an edit in the PATH environment variable in "My Computer>Properties>Advanced Settings". In order to check the functionality, I started cmd-prompt and types
C:>gcc
This gives the following output: ACCESS IS DENIED
I know it's a security permission problem, but I don't know where exactly to do the change in the security settings. Last time I tried it started showing "error-illegal operation" and some error code.
You can not use gcc in your cmd. you must use it in cygwin shell that installed when you install cygwin
You can use gcc without using the cygwin shell !!
But you will probably end up with some configuration problems,
such as not having the correct dll's in the right places.
Another common error that can occur, is if you have multiple instances
of cygwin installed, the search paths get confused and the gcc system
can't find relevant compiler or tries to use the wrong compiler.
All the same this is very possible, I've done it myself hundreds of time,
when testing compilers I have written.
You might want to try invoking 'cc1.exe' for c, or 'cc1plus.exe' for c++,
remember to copy the cygwin dll's into the same directory as cc1 or cc1plus,
Other than that it could be your user account permissions.
Hope this helps.
/Tony

How do I debug existing C programs with Visual Studio 2010 Professional?

how can I use the Visual Studio Debugger on existing C programs I found in a textbook? I want to debug these little examples one by one, but without the overhead of creating a full project for each example.
Example: Let's say I compile "helloworld.c" from the Visual Studio command prompt ("cl.exe helloworld.c"). This gives me helloworld.obj and helloworld.exe. I would like to know if there is a way to use the VS debugger on "helloworld.exe". So far, I have only worked with the debugger on full-blown projects; I have no idea how to debug small "stand-alone" test programs without the Visual Studio project overhead. (I hope this is not a dumb question, as the VS Debugger might only be available for the full project.)
Thank you for any ideas.
Why don't you create one project for testing the sample codes? You can create a single .c-file for all the samples. This would look something like
void sampleA()
{
//hello world
}
void sampleB()
{
//hello everybody else
}
void main(int argc, char** argv)
{
// sampleA();
sampleB();
}
What I do when I have to use VS is to reuse the plain.c project I created a long time ago.
Just paste new code there and go. Throw the code in another file if you want to keep it.
If you're set on not using projects then you can do this. It just gets more difficult as the number of code files increases.
First, to do any meaningful source-level debugging, you will need to generate debug symbols. Add the "/Zi" switch to your cl.exe command line:
cl.exe /Zi helloworld.c
This will generate helloworld.exe, helloworld.obj, and helloworld.pdb (these are the debugging symbols).
Next you will open Visual Studio 2010. Use File -> Open -> File... and select helloworld.exe. It will generate a wrapping solution with one project for your EXE file.
In Solution Explorer, right click on the EXE file and select Debug -> Step Into new instance.
It should pop up a source window and show you the first line of your program. Now, debug away!
The debugger is part of Visual Studio, so you'll have to start it up anyway. However, for the debugger to work properly with your source code, you need to also generate a PDB file (see here). You get this by including /Zi or /ZI as argument to cl.exe (see here).
It is not impossible, but you'll have to learn a lot more about how to exactly build a program from the command line to get the best debugging experience. There are a bunch of options that are real time savers and greatly improve the odds that you'll discover bugs.
Which is a little beside the point right now, learn C instead of spending time learning a boring tool with way too many options. Creating a new project takes 5 seconds after a wee bit of practice. Use the Win32 Console Application project template.
One small setting you have to change if you want to compile as C instead of C++. Right-click the project, Properties, C/C++, Advanced, Compile As = Compile as C Code.
The "overhead" is hardly more effort that it took to post this question!
Simply start with the "Empty Project" template and add the single source file you wish to debug by right-clicking on the project browser sources folder. It takes mere seconds.

Resources