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

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.

Related

How do you debug C code in visual studio code

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.

VS2019: Include file as project macro

I got a makefile project for Linux and I want to compile it on Win10 with Visual Studio 2019.
There are some paths to files defined in the makefile as preprocessor defines.
I have to replace the paths by own created files because they are a kind of PLATFORM_HEADER and I have to adapt a new one to Windows. In the code it looks like:
#include PLATFORM_HEADER
The onliest I tried is to add a property sheet to my project and add a macro:
macro page of property sheet But this macro is not found in the project.
Changing the code is not possible because it is third party code and it must be able to become updated at later times without doing changes again.
In other instructions there is noted that a line called 'inherited property sheet' has to be modified in the project. But in VS2019 this line does not exist.
Thank you for your help!
This sort of problem will be handled by your compiler suite. Probably you'll deal this with your build process manager (make, bitbake, cmake...). You can ask it to pass these #defines as argument for the compilation (-D name=definition).
Now you've unlocked the "very most of fun" if you compile something meant for linux on windows since there are many way that can go wrong. You might want to do a full check of your environment variable when compiling and make sure they point to the right system librairies.
Probably it's worth giving a try to the windows subsystem for linux and other bindings / emulator. If you want to preview a week of work's outcome, maybe you can do it in a linux VM? or just get rid of windows one good time for all :)

how do you create a .c file in Visual Studio 2017 Professional

I am extremely new to programming and have found that a good majority of my mentors use Visual Studio to code in C.
I would like to get an early start using SAMS C Programming.
Can anyone please tell me how to create a .c file in VS 2017? I am only able to create a .cpp file at this time.
Thanks
What you most likely actually want to do is create a project. Go to File-> New-> Project. Most likely, the first programs you will write are console applications (meaning they appear on that screen that resembles an old computer monitor, or what some computer screens look like when booting). If you want to create a console application, go from Project to Visual C++-> Windows Desktop-> Windows Console Application. Ignore any additional pop-up windows and just keep clicking continue. A file called main.cpp will automatically be generated. This is the file you will want to use for your code examples (but DO NOT DELETE the #include "stdafx.h" line, even if your example code does not have it).
Visual Studio compiles both .c and .cpp files as C++ by default. C++ has nearly everything that C has, but may take longer to compile when dealing with huge projects. If you want to use strictly C with no C++, then go to Project (a separate project tab on the same toolbar as the file tab -- it isn't within the file tab)-> ProjectName(Your Project Name) Properties-> Configuration Properties-> C/C++-> Advanced and select "Compile As". You should see a square with a upside-down "^" in it. Click that and select "Compile as C Code (/TC)". The "/TC" is just what you would type in if you were using command-line arguments instead of the convenient window that appears for you to modify everything. Finally, rename main.cpp to main.c.
Since you're learning C, I would also like to mention that I started making a C tutorial on my web site (and if anybody else also happens to see this post and the tutorial, criticize the tutorial as harshly as you can in the comments below).
Click on File, Choose Project --> Visual C++ --> Windows Desktop and under Windows desktop Choose Windows desktop wizard and give project name in the bottom then click on OK. a pop of Windows Desktop Project will come, check empty project and uncheck all options.then click on OK after that in the right side along with your project name more options will come..right lick on source file-> add -> new items -> choose c++ plus file but in bottom give file with extension .c..and you are done .enjoy coding
If you are truly interested in learning to program in C, then leave the VS Code IDE for later and learn to Build C/C++ code on the command line. VS provides a reasonably good compiler (cl.exe) and pre-configured command line (command prompt) windows to build both 32 and 64 bit applications.
The benefit to using the command line is learning what the compiler options are and what they actually do. Simply open a VS command line window and type cl /help. The compiler will list all options along with a short description. There are a great number of resource for command line building that can be accessed through the link above.
A second benefit is you can rapidly compile all your beginning C examples without worrying about projects, etc.. Just cl /nologo /Wall /Ox /Foname.o /Fename.exe /Tc name.c and you are done. (you will want to suppress unneeded warning with individual /wdXXXX options where XXXX is the warning to suppress) 50 Times faster than using the IDE.
Once you are proficient in using the command line, you will have a much easier time transitioning to the IDE -- because you can properly configure the IDE with the appropriate options for your build and you will know what the IDE is doing under the hood (or should be doing under the hood)

Unable to start program, cannot find the file specified, Visual Studio 11

I'm working off of a fresh install of VS 11 Beta, on a Windows 7 64-bit machine. When I try to debug, it gives me this error message.
"Unable to start program 'C:\Users\myname\documents\Visual Studio 11\Projects\Project1\Debug\Project1.exe"
I'm almost entirely sure it isn't a problem with my code--it works fine on online compilers, making me think that it's an issue with how I'm saving it. Just in case, here it is.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("Hello, world!\n");
return 0;
}
My process for creating the project/file is to create a new empty Visual C++ project, then a .cpp file, write my code, save it as a .c inside of the first folder I'm prompted with, within the projects folder, and then try to debug.
What am I doing wrong, here? I know that the debug folder is empty, but I can't figure out how to create the .exe file that should reside in it.
NB: I'm rather new to programming. If answers could be phrased in babyspeak, that would be appreciated.
Well, there could be a number of things wrong. However, based on your description of making a ccp file and a c file, I'm betting that it's a problem in your VS 11 project rather than the code itself. My guess would be that either the c file didn't get included in your project (check the Solution Explorer) or that you have the main (or _tmain) function defined in both the cpp and the c files, which will make the linker issue an error.
There are a bunch of "Hello World" tutorials online for VS 2010 and earlier, but there don't seem to be many for VS 11 Beta yet. You may want to take a look at the 2010 versions. I found this video tutorial that you may want to check out.
Check the output directory. And you can also set manually an output direcotry to save the built exe file.
I know this problem was from May but I thought I would answer with a solution that worked for me:
I was creating a .cpp file in a blank project and was getting a similar error. I noticed that the file had a name but was not showing the extension .cpp. I added the extension to the file (ex3.18.cpp) and did not get the .exe error again.
I don't know if that makes sense with your issue but I worked on this for two days uninstalling and reinstalling VS 11, and this was what finally worked.

What is a good way to build a lot of small tools in Visual Studio?

Suppose you have some source code that comes from the unix world. This source consists of a few files which will create a library and a lot of small .c files (say 20 or so) that are compiled into command-line tools, each with their own main() function, that will use the library.
On unixy systems you can use a makefile to do this easily but the most naive transformation to the windows / Visual Studio world involves making a separate project for each tool which, although it works, is a lot of work to set up and synchronize and more difficult to navigate at both the filesystem and project/solution level. I've thought about using different configurations where all but one .c file are excluded from the build but that would make building all the tools at once impossible.
Is there a nice way of building all the tools from a single "thing" (project, msbuild file, etc.)?
I'm really not interested in using cygwin's gcc/mingw or NAnt. I'd like to stick with the standard Windows toolchain as much as possible.
You don't HAVE to use visual studio to compile code. You can make your own batch file or Powershell script that simply calls the compiler on your source, just like a makefile.
So I've been looking into this for a while now and the solutions all leave much to be desired.
You can...
Create a lot of small projects by hand.
Use MSBuild and deal with its steep learning curve.
Use a build tool that does not integrate well with Visual Studio, like GNU make.
You can't even make a project template like you can with .NET projects! Well, you can make a wizard if you want to wade through the docs on doing that I suppose. Personally, I have decided to go with the "many small projects" solution and just deal with it. It turns out it can be less horrible than I had thought, though it still sucks. Here's what I did in Visual Studio 2008:
Create your first Win32 command line tool project, get all your settings down for all platforms and make sure it works under all circumstances. This is going to be your "template" so you don't want to edit it after you've made 20 copies.
(optional) I set up my paths in the visual studio project files so that everything is built in the project directory, then I have a post-build step copy just the dll/exe/pdb files I need to $(SolutionDir)$(OutDir). That way you can jump into a single directory to test all your tools and/or wrap them up for a binary distribution. VS2008 seems to be insane and drops output folders all over the place, with the default locations of Win32 and x64 output differing. Spending a few minutes to ensure that all platforms are consistent will pay off later.
Clean up your template. Get rid of any user settings files and compiler output.
Copy and paste your project as many times as you need. One project per tool.
Rename each copied project folder and project file to a new tool name. Open up the project file in a text editor like Notepad++. If you have a simple, 1-file project you'll need to change the project name at two places at the beginning of the file and the source code file name(s) at the end of the file. You shouldn't need to touch the configuration stuff in the middle.
You will also need to change the GUID for the project. Pop open guidgen.exe (in the SDK bin directory) and use the last radio button setting. Copy and paste a new GUID into each project file at the top. If you have dependencies, there will be one or more GUIDs at the bottom of the file near the source code. Do NOT change them as they are the GUIDs from the dependencies and have to match!
Go into Visual Studio, open up your main solution and add your tool projects.
Go into the configuration manager and make sure that everything is correct for all supported platforms, then test your build.
It's not beautiful, but it works and it's very much worth the setup time to be able to control your builds from the GUI. Hopefully VS2010 will be better about this, but I'm not too hopeful. It looks like MS is giving a lot more love to the .NET community than the C/C++ community these days.
If you have a makefile you can use a 'makefile' project in Visual Studio (which in misnamed - it simply allows you to specify custom build/debug commands), and use it to invoke GNU make.
You will need to change the makefile to use the VC++ command line tools instead of cc or gcc or whatever it uses, but often these are specified by macros at the top of the makefile.
If the makefile uses other Unix specific commands (such as rm), you may need to make modifications, or create bath files to map commands to Windows equivalents. Another option is to install any necessary tools from GNUWin32 to make it work.
If the build is very complex or involves configure scripts, then you have a harder task. You could generate the makefile from a configure script using MSYS/MinGW, and then modify it as above to make it work with VC++.
Makefile projects will not be as tightly integrated in Visual Studio however. All the build management is down to you and the makefile.
If you're really using Visual Studio, I would suggest creating a project for each tool, and adding these projects to a single solution. From Visual Studio, it's easy to build a complete solution all at once, and MSBuild knows how to build .sln files as well.
msbuild myslnfile.sln
or even:
msbuild
... will build your solution.

Resources