#C in VSCode
170 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
You need to provide more details about what the issue is and what you've already tried.
Sorry for my vocabulary, I'm a beginner. Basically, I've already tried coding on CodeBlocks and it works. But I want to code on VSCode because it looks nicer. Here's what it tells me when I click on run. I'm sorry if I'm explaining poorly, but I really don't understand anything; I don't know anything about C. I'm so lost...
do you have gcc installed
run gcc -v in the terminal and share the output here @lone helm
ok you have gcc lemme see
the code seems good have you installed these extensions?
the C/c++, intelicode and cmake @lone helm
What does "Show Errors" tell you?
idk
debugging in this stupid weird
Looks fine to me. What about launch.json?
ok go in the terminal cmd or powershell and type main.exe maybe that will work
the integrated terminal
doesnt work
you have the latest version of vs code right maybe thats the issue
Wait the exe doesn't even work?
Try compiling manually: gcc main.c -o main.exe then run that exe
did it work
ok delete that file and type this command gcc main.c -o main.exe in the integrated terminal
the same command raccoon gave
when thats done type main.exe in the terminal again
if it didnt share the results over here
its tells me :
main.exe: The term 'main.exe' is not recognized as the name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
- main.exe
-
+ CategoryInfo : ObjectNotFound: (main.exe:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
did it not compile than
Yeah in your screenshot it doesn't even look like gcc generated anything. How did you install gcc?
isnt there a main.exe file next to main.c?
no
idk 😭
it aint compiling than
where did you install gcc from? and give me the link
Try gcc -v main.c -o main.exe
you will probably need to reinstall gcc
ye. I dont remember where did i install it from...
If I have to reinstall gcc, pls help me beacause i dont how to do it
wait i tried something.
it created a main.exe file
What did you do?
Looks like you installed it through something called msys2? Only thing I can think of is it wanting you to run gcc through a specific terminal
i installed this
theres no error but nothing happens
Make sure the output isn't just put in the Debug Console
Try manually doing ./main.exe in a terminal to run the executable
It works !
but does it mean i'll have to type this every time i want to run the code ?
isnt it possible to compile it and run it in one click ?
I don't think so cuz you are using msys also type '!solved' here to end the thread
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity
No, you shouldn't ever have to type it manually, as that's what launch.json is supposed to be for
Clear this file and you should see it autocomplete to a boilerplate (template) one
If not, let me know
The idea with the launch.json being that it'll allow you to recompile + run the program with a single press of the F5 key
i deleted the lauch.json file but it still doesnt work
I said clear, which just means removing all the text inside of it without actually deleting it
oh sry
So the file is empty right now, right? VS Code didn't fill it out with default stuff?
Ok, so remove the file after all. We'll use another approach that does actually fill it out with default stuff
Try it again and make sure it didn't give you a list of languages to choose from like so on my computer
You might just be missing the C++ extension
If you don't see it, download the official C/C++ extension. The little blocks button you see on the left here is the extensions tab, where you can download extensions.
okay yeah nvm the extension is just crap, sorry
I expected it to do at the bare minimum something like launch an executable for you
give me one sec and I'll just send you a boilerplate one
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Program",
"type": "cppdbg",
"request": "launch",
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/main.exe"
}
]
}
enjoy
thx
You can add arguments to the program with the args JSON key, should you want to send arguments to your program
when i press F5 it works
what does that mean ?
So if you want F5 to do ./main.exe foo bar, where foo and bar are arguments to your program that you can read from argv in main(), you can put those arguments in the launch.json if you add an "args" key. If you type "args" next to the "program" key in the JSON file it autocompletes it for you.
ooohhhh ok
So right now F5 is launching the main.exe you built, but it isn't recompiling it for you. You need to add the preLaunchTask in order to tell it which tasks.json task you want to run to build the executable.
(I am presuming you have a tasks.json that contains a compilation task)
okay yeah you do. I'd change the label name in the tasks.json to something shorter like build
wait what, if i want to compile it and run it with F5. How can i do that ?
Then you add "preLaunchTask": "build" in your launch.json in order to tell VS Code that you want to run the build task before running the program
like that ?
exactly
ok lemme try
now just let its task in the tasks.json have the same label
so it knows what it is referring to
wait i tried to F5 and its telling me could not find the task build
yeah, see this
it is telling you "there is no task called 'build', please rename your task to that"
read my messages again and let me know what is confusing
i'm so sorry, what do i need to do ? 😭 I need to rename my tasks.json to build.json ?
look inside your tasks.json
there is a key there called "label" with a very long name as a value
that name needs to match the name you are looking for with your preLaunchTask key in the launch.json
"preLaunchTask" literally means "before launching my program, run this (build) task for me", so you tell it what the name of the build task is. You called it build
So you just need to make sure the label of your task matches
ayy nice
Can you show your tasks.json, I want to show something neat
Oh nvm you already have -g as an arg
Okay, I just wanted to say that you should now be able to go to your main.c, and press just to the left of one of the line numbers in order to place a breakpoint there
So try that out. Put a variable in there, put a breakpoint on the line after it, and check if when you press F5 the debugger pops up. You should see the variable's current value in the debugger menu on the left
wait, i'm so sorry i'm a beginner... How do i put a variable ? i'm not sure
you can make an integer variable for example with int foo = 42;
so put that inside of the main()
ok and then i put a break point and run it with F5 ?
yeah, breakpoint on the next line
yeah, so put the breakpoint on the printf()
so see that "watch" tab on the left that is collapsed? expand that by clicking on it
and the "variables" one
weird that they were collapsed
yup
okay, to show its true power, add the line x = 42; after the one where you initialize it to 10
set a breakpoint on the x = 42; line
Then press either of these buttons at the top of your screen, and you should see the value of x change from 10 to 42 in the debugger's "variables" tab on the left
hover over it to get an explanation, idk
you might just need to close and reopen the file to see the error disappear
great
so you can't know this yet, but I've been programming for over a decade now (high school, university, etc.), and learning the basics of using any debugger, like this one, is unironically the most useful skill a programmer will ever learn
the reason it's so great is that you can just step through every line of code as the computer is executing it, to see how values are changing, which helps immensely with gaining intuition for what a program is doing, and for figuring out what is causing a bug
so congrats!
you can explore the other buttons at the top, but in my experience I only ever really use these
the left one means "jump over this function call", while the one on the right means "step into this function call". So if you're currently just on a regular line of code like x = 42;, they act identically, since there is nothing to step into
idk if you've written a function yet, but even if you haven't, you can remember that you can step into it with the button on the right for later