简体   繁体   中英

Setting up environment for learning C

I am trying to learn C/C++ programming by following the MIT OCW materials. I am running a Windows PC and the course material mandates running all C/C++ programs under the Linux environment with the gcc / g++ compiler (for C and C++). They also require the use of gdb and valgrind as a debugger.

I have already installed gcc / g++ from the MinGW package and am wondering if there is a specific instructions on how I can achieve the setup. For now I'd like to be able to set up the Linux, gdb , and valgrind and at least write a simple program and compile and run it in the Linux environment. For now I've also installed VirtualBox but for some reason the virtual machine I created always gives the following message: FATAL: No bootable medium found! System halted FATAL: No bootable medium found! System halted .

I guess I am just lost in all the software packages/tools that are needed in order to start the learning. I've only used Microsoft Visual Studio before and so these whole new command prompts and tools are really confusing to me. Would be great if someone could give me the specific instructions on how I can start from a Windows PC and arrive at compiling and debugging a simple C program with gdb and valgrind in the Linux environment.

I recommend you use VirtualBox to create a virtual machine. This is best because it sandboxes your development environment and you get a real Linux environment to work in. VirtualBox is free and open source and you probably won't need any advanced features you might see in VMWare or Parallels.

Now that you have the environment built you can create a directory where your project folders go. From the prompt just do mkdir projects , cd projects , mkdir helloworld , cd helloworld . Then, you can use the built-in editor nano to edit files. Type nano hello.c and then enter the following:

#include <stdio.h>
int main()
{
    printf("Hello, world\n");
}

Then type Ctrl-O to write out and then Ctrl-X to exit.

Then you just need to install gcc and I would suggest installing make as well:

$ sudo apt install gcc
...
$ sudo apt install make

Now to compile and test your first program in your development environment:

$ make hello
$ ./hello

Then you should see Hello, world on your screen.

Valgrind and Un-mount Disk

From your helloworld project folder enter sudo apt install valgrind , then run valgrind ./hello .

Finally, go to Settings -> Storage and un-mount the installation ISO.

SSH Access

Install openssh-server using the following command:

sudo apt install openssh-server

Find the IP address of your Ubuntu host by typing ifconfig . Then for VirtualBox go to Settings :: Network :: Advanced and click Port Forwarding . Use these settings:

Host IP: 127.0.0.1
Host Port: 22
Guest IP: (IP of Ubuntu VM)
Guest Port: 22

Now you can ssh to your Ubuntu VM and also use tools like scp .

Shared Folders

Shared folders allow you to have a medium that spans both file systems, allowing you to share files between the two environments. This resource offers a lot more detail in the different methods: https://www.virtualbox.org/manual/ch04.html#sharedfolders . I will go over how to set this up quickly in the setup detailed here.

The following will install the Linux headers required for VirtualBox shared folders:

sudo apt-get install build-essential linux-headers-`uname –r`

Then go to the Devices tab of the VirtualBox menu and click Insert Guest Additions CD image... .

Now we need to mount the cdrom and run the script:

sudo mount /dev/cdrom /media/cdrom
sudo /media/cdrom/VBoxLinuxAdditions.run

Figure out what Windows folder you want to share, and share it by going to VirtualBox guest Machine :: Settings :: Shared Folders and add it with the options Auto-mount and Make Permanent . When you're done, do a sudo reboot .

The shared folders are automatically added and exist in /media/sf_* .

You must be in the vboxsf group to work with those files. Use this command to add a user testuser to that group:

sudo usermod -aG vboxsf testuser
sudo reboot

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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