Compile C program with visual studio - c

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

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

Include ext4.h in C program

I'm trying to create a little program that can read the ext4 filesystem, and for that i need to use all the structures that are defined in the ext4.h file.
Problem is, that this file includes a set of kernel headers such as
#include <linux/blkdev.h>
#include <linux/magic.h>
#include <linux/jbd2.h>
#include <linux/quota.h>
#include <linux/rwsem.h>
#include <linux/rbtree.h>
#include <linux/seqlock.h>
#include <linux/mutex.h>
#include <linux/timer.h>
#include <linux/version.h>
... and more
I've installed the kernel headers using:
$ apt search linux-headers-$(uname -r)
So now i have them in /usr/src/linux-headers-5.4.../include and i try to compile my program with -I /path/to/kernel-headers but now it seems that there're headers included in those headers that it can't find such as asm/current.h which in my filesystem is asm-generic/current.h
Do someone that ever did something like this can help me?
I suggest you take a look at e2fsprogs. This is the user-space toolset for manipulating ext2/3/4 filesystems, and it contains a userspace implementation of the filesystem you can adapt.

Do I need to include system header file in the source if another header file already includes it?

For example, in the header file example.h, I wrote:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
And in the source file example.c, I wrote:
#include "example.h`
Do I still need to explicitly wrote these lines if I need functions of these libararies?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Thanks!
No.
Keep in mind that includes works like some kind of text substitution, as it's done by the preprocessor.
When you write, on some file:
#include "someheader.h"
It's just like that line will be replaced with the actual content of the header file.
No, you don't.
Include will, as it's named, include the whole content of your header file in your .c file.
If you are using linux, try cpp example.c or gcc -E example.c to see what #include does. You will run the c-preprocessor on your file, which is the program that interpret all # started instructions before the copilation

Libraries not found in Cygwin

When trying to compile this program:
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
printf("Hey there\n");
return 0;
}
i get compiler-error:
test.c:1:24: netinet/in.h: No such file or directory
test.c:2:24: sys/socket.h: No such file or directory
test.c:3:19: netdb.h: No such file or directory
I use Cygwin (in Windows). Seems the compiler doesnt find any of the libraries exept a few core ones (stdio, string..., stdlib also works...). Is this a linking issue or does it have something to do with my installation of Cygwin? Do i have to specify that i want these libraries included when installing Cygwin? Please help, i'm dying here...
You'll need to run cygwin's setup.exe again, and find the packages that contain the headers you need. Maybe there's a unix net package somewhere.
You might want to try MingW (http://www.mingw.org).

Resources