#C in VSCode

170 messages · Page 1 of 1 (latest)

lone helm
#

Please help me, I just want to code in C on vscode but I can't manage it. I've watched 20,000 tutorials but still don't understand why it's not working. It also doesn't work on Microsoft Visual Studio. The only IDE that works is CodeBlocks, but I really prefer VSCode.

rocky mulchBOT
#

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.

hearty lion
#

You need to provide more details about what the issue is and what you've already tried.

lone helm
#

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...

thorny pilot
#

run gcc -v in the terminal and share the output here @lone helm

lone helm
thorny pilot
#

ok you have gcc lemme see

#

the code seems good have you installed these extensions?

#

the C/c++, intelicode and cmake @lone helm

lone helm
#

I have these. But i dont find the intelicode extension

#

oh sry

hearty lion
#

What does "Show Errors" tell you?

lone helm
#

i found it

#

it shows me this

thorny pilot
lone helm
#

idk

hearty lion
#

How helpful of VSCode

#

Could you send your tasks.json?

thorny pilot
#

debugging in this stupid weird

lone helm
hearty lion
#

Looks fine to me. What about launch.json?

thorny pilot
# lone helm

ok go in the terminal cmd or powershell and type main.exe maybe that will work

#

the integrated terminal

lone helm
thorny pilot
hearty lion
#

Wait the exe doesn't even work?

#

Try compiling manually: gcc main.c -o main.exe then run that exe

thorny pilot
#

did it work

lone helm
#

no
my main.exe file is empty

#

i swear i'm lost 😭

#

i think my pc is broken

thorny pilot
#

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

lone helm
thorny pilot
#

ye now run that main.exe

#

in the terminal by typing "main.exe"

lone helm
#

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
thorny pilot
#

did it not compile than

hearty lion
#

Yeah in your screenshot it doesn't even look like gcc generated anything. How did you install gcc?

thorny pilot
#

isnt there a main.exe file next to main.c?

lone helm
#

no

thorny pilot
#

it aint compiling than

thorny pilot
hearty lion
#

Try gcc -v main.c -o main.exe

thorny pilot
#

you will probably need to reinstall gcc

lone helm
lone helm
#

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

hearty lion
#

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

lone helm
#

Now when i run the code this happens

lone helm
#

theres no error but nothing happens

rain minnow
#

Try manually doing ./main.exe in a terminal to run the executable

lone helm
#

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 ?

thorny pilot
rocky mulchBOT
#

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

rain minnow
rain minnow
# lone helm

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

lone helm
#

i deleted the lauch.json file but it still doesnt work

rain minnow
#

I said clear, which just means removing all the text inside of it without actually deleting it

lone helm
#

oh sry

rain minnow
#

So recreate launch.json

#

No worries

lone helm
#

and when its cleared ?

#

what do i do next ?

rain minnow
#

So the file is empty right now, right? VS Code didn't fill it out with default stuff?

lone helm
rain minnow
#

Ok, so remove the file after all. We'll use another approach that does actually fill it out with default stuff

lone helm
#

it created this file

rain minnow
#

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.

lone helm
#

when i clicked on the create json file i chose C++(GDB/LLDB)

#

i have the extension

rain minnow
#

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

lone helm
#

thx

rain minnow
#

You can add arguments to the program with the args JSON key, should you want to send arguments to your program

lone helm
#

when i press F5 it works

rain minnow
#

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.

lone helm
#

ooohhhh ok

rain minnow
#

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)

rain minnow
# lone helm

okay yeah you do. I'd change the label name in the tasks.json to something shorter like build

lone helm
#

wait what, if i want to compile it and run it with F5. How can i do that ?

rain minnow
#

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

lone helm
#

like that ?

rain minnow
#

exactly

lone helm
#

ok lemme try

rain minnow
#

now just let its task in the tasks.json have the same label

#

so it knows what it is referring to

lone helm
#

wait i tried to F5 and its telling me could not find the task build

rain minnow
#

it is telling you "there is no task called 'build', please rename your task to that"

lone helm
rain minnow
#

read my messages again and let me know what is confusing

lone helm
#

i'm so sorry, what do i need to do ? 😭 I need to rename my tasks.json to build.json ?

rain minnow
#

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

lone helm
#

ohhhhhh ok

#

it works

rain minnow
#

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

lone helm
rain minnow
#

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

lone helm
#

wait, i'm so sorry i'm a beginner... How do i put a variable ? i'm not sure

rain minnow
#

you can make an integer variable for example with int foo = 42;

#

so put that inside of the main()

lone helm
#

ok and then i put a break point and run it with F5 ?

rain minnow
#

yeah, breakpoint on the next line

lone helm
#

oh the next line

#

wait

rain minnow
#

yeah, so put the breakpoint on the printf()

lone helm
rain minnow
#

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

lone helm
#

ohhh i see

#

there s x:10

rain minnow
#

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

lone helm
#

why is it showing me an error ?

rain minnow
#

hover over it to get an explanation, idk

#

you might just need to close and reopen the file to see the error disappear

lone helm
#

ohhhhhh it works

#

ok

#

yes it changes from 10 to 42

rain minnow
#

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!

lone helm
#

wow, that's aswome

#

tysm

rain minnow
#

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

lone helm
#

ooh ok i see

#

cool

#

thanks for helping me 👍