Visual Studio 2019 custom shortcut - c

In a visual studio project it often happens to create several files and write a bunch of lines to include the necessary libraries / headers. I was wondering if it was possible to automate the process with a custom shortcut; for example, in my case, I would like to insert the following lines:
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
for example pressing CTRL+MAIUSC+M. Is that possible?

Related

Eclipse "error: no such file or directory" while the header does exist

I'm working on automatic unit testing of ESP32 code in eclipse 2022-06 using google test.
For this I have made a new project and connected it with the ESP32 code using a header file.
I have also connected the unit test project with the ESP32 code via
properties > C/C++ General > Paths and Symbols.
As seen in the picture below.
When running the ESP32 code I get no errors but when I try to run the unit test I get "fatal error: freertos/FreeRTOS.: No such file or directory". However this path can be found in the EPS32 project that im testing.
Does anyone know how to solve this?
#include "main/main.h"
Part of the unit test code
#ifndef MAIN_DOT_H
#define MAIN_DOT_H
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "rom/ets_sys.h"
#include "rom/gpio.h"
#include "esp_intr_alloc.h"
#include "esp_attr.h"
#include "driver/timer.h"
#include "esp_task_wdt.h"
#include "soc/timer_group_struct.h"
#include "soc/timer_group_reg.h"
#include "driver/mcpwm.h"
Part of the header file
#include "main.h"
Part of the ESP32 code
To repeat the question, how can I solve this error?

C #include errors in VS Code, cannot open libraries

I use Ubuntu 18.04, VS Code and C/C++ extension and I was trying to test the code from this other question but got the following error:
#include errors detected. Please update your includePath. Squiggles are disabled for this translation unit (/home/alan/Desktop/C/test/main.c).
Also, it says that it can't open source files from "conio.h" to "iostream.h"...
I tried using vcpkg as VSC suggests but it didn't work :/
The first 3 libraries are okay as always, can anyone help me with the others? Thanks!
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <conio.h> //Can't
#include <graphics.h> //open
#include <dos.h> //those
#include <iostream.h> //four

Xcode for C Implementation - Random Generator - Environment Problems

When you have a few files (see below), how do you get them all working inside Xcode for a C implementation?
These are the files that I’ve been given for this project:
pcg_basic.c
pcg_basic.h
pcg32-demo.c
Project guidelines:
http://www.pcg-random.org/using-pcg-c-basic.html
I am also unclear what should be copied to the main.c file. Can't get a grasp on this.
Apparently you also need to link the code with the pcg_basic.o.
(not sure how to do that, either)
I know the code does work because it was developed by an expert in this space. But everything I try in Xcode ("fails to build"), so I presume it must be the way I've set things up.
What am I doing wrong?
*Student here. I am totally new to programming. Thanks!
error snippet 1
error snippet 2
CODE
/*
* PCG Random Number Generation for C.
* For additional information about the PCG random number generation scheme,
* including its license and other licensing options, visit
* http://www.pcg-random.org
*/
/*
* This file was mechanically generated from tests/check-pcg32.c
*/
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include <string.h>
#include "pcg_basic.h" <---pcg_basic.h not found
To setup:
in Xcode <File/New/Project...>
choose macOS template tab
from there choose Command Line Tool
in the Wizard choose C as language
Drag & drop your files (pcg_basic.h, pcg_basic.c, pcg32-demo.c) to the Xcode project navigator on the left where main.c resides. A dialog appears: make sure to check Copy items if needed. Then delete main.c in Xcode.
The program builds and runs then.
Demo

Why no <string.h> in getopt library?

I have been using Ludvig Jerabek's port of the GNU getopt on Windows and getting errors on lines like:
if (d->optind != argc && !_tcscmp(argv[d->optind], _T("--")))
_tcscmp is a macro that resolves to strcmp on my system and then it reports strcmp not found. If we examine the headers in getopt.cpp:
/* Getopt for Microsoft C
....
Date: 02/03/2011 - Ludvik Jerabek - Initial Release
....
Revisions:
....
08/09/2011
....
#include <stdlib.h>
#include <stdio.h>
#include "getopt.h"
We see the problem: <string.h> is not included. I guess in Visual Studio <string.h> is included automatically maybe? I know I have successfully built getopt.cpp in Visual Studio, but using a manual environment with gcc on MinGW it is complaining about all the string compare functions being missing. What is the explanation for this?
The real tchar.h causes either #include <string.h> or #include <mbstring.h> depending on the character-set macros.
You have a broken version of tchar.h that does not correctly emulate these Microsoft-specific "Generic-Text Routine Mappings".

Compile C program with visual studio

I am required to explain what the program located in the following links does:
main.c
csapp.c
csapp.h
I compile the following code in linux as:
............................................................
(note all three files have to be in the same working directory for compilation to work. )
that command is: gcc main.c csapp.c
when I execute that command I get the executable: a.out and I get no compilation errors!
That executable file can be downloaded from here (I don't think you need that file plus I will not execute that file if I where you).
Anyways I think that if I could debug the program I will be able to understand better what is going on. As a result I have created a C++ console empty console project in visual studio. I will like to include the same files in there and be able to compile it. I have never used c++ before and I don't really understand where to place header files. This is what I have done hoping to be able to compile the program:
The program will not compile if I place the files like that.
I have also tried placing all the files in the same directory just like on the linux virtual machine:
that does not compile either.
How will I be able to compile that program with visual studio?
If you look at csapp.h you'll notice it tries to include these headers:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <math.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
Some of these are std C headers, but others are specific to POSIX/Linux/UNIX style operating systems (pthread.h for example). You won't be able to use these libraries on Windows or in Visual Studio unless you're doing something unusual like compiling against Cygwin libraries.
If you want to get an understanding of what the program is doing, there's a number of things you could do. First off, just read through the code and look up the functions it calls in the man pages which document those functions (If you have gcc, I guess you also have man?) second, yes you could print to console to figure stuff out. You could also use a debugger like gdb to step through the program, it's not as intuitive as VS debugger but it works...

Resources