简体   繁体   English

使用visual studio编译C程序

[英]Compile C program with visual studio

I am required to explain what the program located in the following links does: 我需要解释以下链接中的程序:

main.c main.c中

csapp.c csapp.c

csapp.h csapp.h

I compile the following code in linux as: 我在linux中编译以下代码:

................................. ................................. 在此输入图像描述 ........................... ...........................

(note all three files have to be in the same working directory for compilation to work. ) (注意所有三个文件必须位于同一工作目录中才能进行编译。)

that command is: gcc main.c csapp.c 该命令是: gcc main.c csapp.c

when I execute that command I get the executable: a.out and I get no compilation errors! 当我执行该命令时,我得到了可执行文件: a.out ,我没有编译错误!

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. 因此,我在visual studio中创建了一个C ++控制台空控制台项目。 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. 我之前从未使用过c ++而且我真的不知道在哪里放置头文件。 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: 我也尝试将所有文​​件放在同一目录中,就像在linux虚拟机上一样:

在此输入图像描述

that does not compile either. 那也不编译。

How will I be able to compile that program with visual studio? 我如何能够使用visual studio编译该程序?

If you look at csapp.h you'll notice it tries to include these headers: 如果你看一下csapp.h,你会发现它试图包含这些标题:

#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). 其中一些是std C头,但其他一些是特定于POSIX / Linux / UNIX样式的操作系统(例如pthread.h)。 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. 您将无法在Windows或Visual Studio中使用这些库,除非您正在做一些不寻常的事情,例如编译Cygwin库。

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. 首先,只需阅读代码,然后在文档中查找它调用的函数(如果你有gcc,我猜你也有男人?)第二,是的,你可以打印到控制台来解决问题。 You could also use a debugger like gdb to step through the program, it's not as intuitive as VS debugger but it works... 您也可以使用像gdb这样的调试器来逐步执行该程序,它不像VS调试器那样直观,但它可以工作......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM