I have this simple program called main.c:
#include <stdio.h>
int main(){
puts("hi");
puts("bye");
return 0;
}
and this makefile:
run: main.o
gcc main.o -o run
main.o: main.c
gcc -c -g $< -o $#
clean:
rm -f run main.o
Now I want to use it in VS 2017. So I opened the program folder in VS (main.c and the makefile are located there). Then I configured these tasks for it:
{
"version": "0.2.1",
"tasks": [
{
"taskName": "BUILD",
"appliesTo": "/",
"workingDirectory": "${workspaceRoot}",
"command": "make",
"type": "default"
},
{
"taskName": "RUN",
"appliesTo": "/",
"workingDirectory": "${workspaceRoot}",
"command": "run",
"type": "default"
}
]
}
What I tried to do next, is to put break point in the main function and then use the RUN task. I thought VS will stop the running on the break point but that didn't happen and the program run till the end of it.
How can I use the task configuration, so that when I click on RUN - it will stop on the break points that I put?
It's important for me to emphasize that I want to debug the program using the task configuration only and not via other ways that VS have.
Related
I have been stuck on this bug for a few hours now.
I wrote some code in C that uses functions and structs from another C file that I wrote. when I run the code manually (with the makefile I wrote) it runs.
the makefile:
exec: ../LineParser.c main_c_file.c
gcc -g -m32 -Wall -c ../LineParser.c -o LineParser.o
gcc -g -m32 -Wall -c main_c_file.c -o main_c_file.o
gcc -g -m32 -Wall LineParser.o main_c_file.o -o main_c_file
rm LineParser.o main_c_file.o
but when I am trying to debug it I get the error:
/dir1/dir2/task2/main_c_file.c:123: undefined reference to imported_function'
collect2: error: ld returned 1 exit status
Build finished with error(s).
* The terminal process failed to launch (exit code: -1).
I tried
adding -g to the make file.
to first compile the code
and then add its full path to the program tag in the debug configuration file
{
linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "by-gdb",
"request": "launch",
"name": "Launch(gdb)",
"program": "/dir1/dir2/main_c_file",
"cwd": "${workspaceRoot}"
}
]
}
by the way I am runing this code in Linux
I am very new to C programming and I just installed mingw and started using vs code. I have installed all teh extension required in the VS code and have selected the default builder as gcc in the vs code.
here is the source code I am trying to run
main.c :
#include <stdio.h>
int main(){
int age;
printf("Hello, world!\n");
printf("Whats your age?: ");
scanf("%i", &age);
printf("You are %i years old", age);
return 0;
}
here is the tasks.json which I created:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\gcc.exe",
"args": [
"-g",
"-fdiagnostics-color=always",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:\\msys64\\mingw64\\bin\\gcc.exe"
}
]
}
But on building the file (ctrl+shift+B), I get the following error:
> Executing task: C/C++: gcc.exe build active file <
Starting build...
C:\msys64\mingw64\bin\gcc.exe -g -fdiagnostics-color=always C:\Users\user name\Desktop\username's folder laptop\College CS\C programming\main.c -o C:\Users\user name\Desktop\username's folder laptop\College CS\C programming\main.exe
gcc.exe: fatal error: input file 'C:\Users\user' is the same as output file
compilation terminated.
Build finished with error(s).
I have tried deleting and recreating the .c file.
It works fine when I type the following command in the cmd manually after going into that folder:
gcc main.c -o main.exe
So, the gcc works fine but for some reason when I mention the whole path, it goes haywire.
Please help me out as I couldn't find any solutions for this error and sorry of this doubt is a silly one.
I was expecting the build to be successful in VS code but it showed a fatal error even though the VS code is open in that particular folder.
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
I installed VScode and gcc on Mac Silicon, tried running the above hello world program in C, but I get the following error:
Undefined symbols for architecture arm64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
When I run the following commands on the terminal I get this:
which gcc
/usr/bin/gcc
which gcc-11
/opt/homebrew/bin/gcc-11
I'm not sure if I installed the compiler incorrectly(first time trying C).
For just starting out, I recommend you just have a simple task like this in tasks.json (Terminal -> Configure Tasks):
{
"tasks": [
{
"label": "Run my C",
"type": "shell",
"command": "gcc -o myoutput ${file} && ./myoutput",
"group": {
"kind": "build",
"isDefault": true
},
}
}
],
"version": "2.0.0"
}
This will compile it and immediately run ./myoutput (inside vscode's terminal). isDefault means that it is the default build task so you can run it by Ctrl+Shift+B (also under Terminal -> Run Build Task).
I like Sublime a lot and wish to execute my program directly from the editor. I've done it with gcc, but now want to use tcc.
I can't find a build system for tcc so I took a C++ build system. There is a problem that it can't find the file I want to execute. Here's my build system:
{
"shell_cmd": "tcc \"${file}\" -o \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c",
"variants":
[
{
"name": "Run",
"shell_cmd": "tcc \"${file}\" -run \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
}
]
}
I changed g++ to tcc and -o to -run but it giving a file not found error.
tcc: error: file 'C:\Users\Paras Ghai\Documents\C Projects/Binary_Search' not found
Here after Documents\C Projects it is showing / in place of \. Is that the problem? How do I fix it?
So I did some changes and finally created a simple build-system for my tcc compiler and here it is->
{
"windows":
{
"cmd": ["tcc","-std=c99" ,"-run", "$file_name"]
},
"selector" : "source.cpp",
"shell": true,
"working_dir" : "$file_path",
}
I hope that it works for other people too.
I wrote this simple program:
main.c:
#include "main.h"
void nothing(){}
int main(){
nothing();
return 0;
}
main.h:
void nothing();
Till now compiled and ran it using this generic Makefile:
CC = gcc
CC_FLAGS = -g
EXEC = run
SOURCES = $(wildcard *.c)
OBJECTS = $(SOURCES:.c=.o)
$(EXEC): $(OBJECTS)
$(CC) $(OBJECTS) -o $(EXEC)
%.o: %.c
$(CC) -c $(CC_FLAGS) $< -o $#
clean:
rm -f $(EXEC) $(OBJECTS)
Now I want to compile and run it with VS, but the compilation and cleaning (by make clean) must be performed by the makefile. So I opened the folder of the program (contains all 3 files) in VS, and configure the following tasks for it at tasks.vs.json:
{
"version": "0.2.1",
"tasks": [
{
"taskName": "BUILD",
"appliesTo": "/",
"workingDirectory": "${workspaceRoot}",
"command": "make",
"type": "default"
},
{
"taskName": "CLEAN",
"appliesTo": "/",
"workingDirectory": "${workspaceRoot}",
"command": "make clean",
"type": "default"
}
]
}
The task for BUILD works, but for CLEAN I get:
'"make clean"' is not recognized as an internal or external command,
operable program or batch file.
Do you know how can I fix my task for CLEAN , so that it will perform make clean like I want?
The string given as the value for "command" is interpreted as a single command. To send arguments to your command, like clean, the schema provides the possibility to give an array of arguments. In your case, it would look like this:
"command": "make",
"args": [ "clean" ],