0

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.

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.

Greg Schmit
  • 4,275
  • 2
  • 21
  • 36
Johnny Chan
  • 77
  • 1
  • 8
  • to get started learning in the mean time, try http://repl.it it is a fully functional series of languages you can use online for free. – cure Nov 04 '16 at 02:02
  • 1
    I don't see what MinGW packages have to do with this scenario. You are not interested in usin those gcc packages under MS-Windows, so why did you install those? You need a working virtual machine (or a physical mache, actually) that runs some Linux distributions. There are ready to be used appliances to download. Then you can use the distributions package management system to install the required packages with a few mouse clicks. But I am afraid we cannot help with your setup if you don't share your current configuration... – arkascha Nov 04 '16 at 02:05
  • you need to mount a linux install disk in virtualbox – Sean D Nov 04 '16 at 02:45

1 Answers1

6

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
Greg Schmit
  • 4,275
  • 2
  • 21
  • 36
  • 1
    Hi Greg, thank you for your detailed instructions! You were very clear and everything worked for me! Now I've played a bit with the terminal commands I can't find where my Download file (am told the path should be /home/user/Downloads) is located. There is only one file under my user name which I created following your mkdir command. I am trying to download sublime text as my main text editor and write code in there and without knowing where my download file is I can not tell if I've downloaded sublime correctly, please help me. Thank you sir :D – Johnny Chan Nov 05 '16 at 13:31
  • 1
    Hi Johnny, because that Linux environment is inside a virtual machine, you don't have any single access methods that span both file systems. To get that, you'll need to use VirtualBox to share a folder between your Windows environment and your Linux virtual environment. There are tutorials, but later I will do this in Ubuntu and post an update with a step-by-step. If you find a tutorial and get it to work before I get to it, feel free to contribute to the community by posting a comment or editing (if you're allowed to) by answer. – Greg Schmit Nov 05 '16 at 22:21
  • 1
    And by the way, I told you to use Ubuntu server because it is a command-line interface which you should get used to working in. However, if you want to know what a Linux graphical interface looks like, feel free to download Ubuntu desktop and install that into yet another VM and run it to get that experience. – Greg Schmit Nov 05 '16 at 22:23
  • 1
    And finally, SSH is another great way to access resources on Linux/Unix servers, and I will add those instructions later. – Greg Schmit Nov 05 '16 at 22:24
  • 1
    Johnny, Sublime has an SFTP plugin that you can use to remotely edit files and you should use that. So you would run Sublime in your Windows environment and edit your Linux files remotely. This first answer to this question is a good place to start: http://stackoverflow.com/questions/15958056/how-to-use-sublime-over-ssh. Then you can make changes, save them, and then jump back to your Linux environment to compile/test them using `make`, `valgrind`, etc. – Greg Schmit Nov 05 '16 at 23:12
  • Greg, thanks for all the above info, I really appreciate it! As I typed the "sudo apt-get install build-essential linux-headers-'uname -r' " (I typed my username instead uname) commands it give me an error message back "E:Unable to locate package linux-headers-uname -r". I tried multiple times but couldn't resolve it on my own so I proceed to attempt mounting cdrom step. A popup window shows and says "Unable to insert the virtual optical disk C:\Program Files.. into the machine Ubuntu and ask if I'd like to try to force the insertion (with force unmount button)". I am stuck at the install. – Johnny Chan Nov 06 '16 at 01:20
  • 1
    No, `uname` should just be `uname`; try typing it verbatim, and the ticks are not single-quotes, but rather the tick mark on the same key as tilde. – Greg Schmit Nov 06 '16 at 01:34
  • After I entered "sudo mount /dev/cdrom /media/cdrom" I get "mount: /dev/sr0 is write-prtected, mounting read-only", the running on VBoxLinuxAdditions went fine with the last line "Could not find the X.Org or XFree86 WIndow System, skipping." – Johnny Chan Nov 06 '16 at 02:28
  • entering "mkdir /mnt/downloads" into the terminal and it says "mkdir: cannot creat directory `/mntmkdir': Permission denied" and "mkdir: cannot create directory `/mnt/downloads': File exists (this is the 2nd time I try this so the file was created already) – Johnny Chan Nov 06 '16 at 02:35
  • Sorry for the prolonged comments, don't really know a better way to ask extended questions here. So I then entered "mount -t vboxsf Downloads /mnt/downloads" and I get the response "mount: only root can use "--types" option". What do I do now master? – Johnny Chan Nov 06 '16 at 02:37
  • Ok, but you should've Google'ed that problem: you don't have permissions. So do `sudo mkdir ...`. – Greg Schmit Nov 06 '16 at 02:51