Setting up environment for learning C - 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.
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.
Download VirtualBox (https://www.virtualbox.org/wiki/Downloads)
Download Ubuntu server (https://www.ubuntu.com/download/server)
Create a new virtual machine (VM)
Mount the Ubuntu ISO file to the VM (Settings -> Storage, check 'Live CD/DVD' and click the disk icon to browse for the ISO)
Boot up the VM and install Ubuntu by following the on-screen instructions
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

Related

Soln for U-Boot Compilation error with myir board?

I'm using the MYD-Y6ULX-HMI series boards supports the Linux kernel version 4.1.15, and provided with rich hardware and software resource. They recommend to use Ubuntu 16.04 64bit distribution. For more details about the devkit
-> http://www.myirtech.com/list.asp?id=602
-> http://d.myirtech.com/MYD-Y6ULX-HMI/
By referring their usermanual,
(1) Installed virtualBox with Ubuntu 16.04 64bit distribution
(2) Installed software package and made the working directory in root.
(3) Installed Yocto toolchain:gcc version 5.3.0(GCC) - for driver(metatoolchain)
After installation sourced the env file:
source /opt/myir-imx6ulx-fb/4.1.15-2.0.1/environment-setup-cortexa7hf-neon-poky-linux-gnueabi
and checked the compiler version:
arm-poky-linux-gnueabi-gcc --version
(4) Now trying to make u-boot config and compiling it with that able to generate config file but facing issue while compiling it,
(a) Sourced the metaToolChain
(b) Entered the U-bootsource file directory
(c) given sudo make distclean
(d) Exported the env variables CROSS_COMPILER and ARCH
(e) sudo make
the core board of one i'm using is MYC-Y6ULY2-356N256D-50-C
(f) sudo make
But cant able to compile. Can any one please help on this to solve the issue?
enter image description here
enter image description here
Do not use sudo here. It's a bad practice all around, but specifically in this case the environment from the user isn't passed to sudo, so CROSS_COMPILE is unset, so that's why you see that error.

How do I install Pact onto Windows?

I was going through the beginner tutorial and came to the Atom SDK page. I have a windows system, so installing brew doesn't work, but the tutorial only gives a way for Linux systems or Mac OS systems to install Pact. Is there any way to install Pact onto windows?
There is no official support for Windows (to my knowledge), but there is a possible workaround that worked for me. The workaround involves Ubuntu and WSL(Windows Subsystem for Linux).
The first step is installing Ubuntu and WSL in your windows device. Those who have done this could move ahead to step two. I followed this Youtube video for setting up WSL
With WSL Terminal set up, go to the terminal home directory and download the compressed pact executable from the official pact releases Github, suiting your ubuntu version, using command line curl. An example command I used for Ubuntu 20.04 and Pact v4.3 is:
curl -LJO https://github.com/kadena-io/pact/releases/download/v4.3/pact-4.3-linux-20.04.zip
3.Install unzip on command line using:
sudo apt install unzip
Unzip the compressed executable to the same directory using the unzip command. ex:
unzip pact-4.3-linux-20.04.zip`
this will add a pact.exe file on that directory
Update the user permissions on the pact.exe file using chmod command to enable read and execute.
chmod u=rx ./pact
Add the directory with pact executable to terminal permanent search path. This is done by exporting a new path in the .bash_profile file. (Editing .bash_profile could be done through any command line text editor e.g. vim). I followed this tutorial to complete this step.
And that should allow you to start pact from a windows wsl terminal.

C Code Won't Compile in MacOS Catalina or Big Sur

I am unable to compile C code on neither MacOS Catalina nor Big Sur. I see the header files present in /usr/include/, but I get errors from my C compiler.
Current error messages I get from the compiler are:
For "#include <time.h>" => error: cannot open source file "time.h"
For "#include <stdint.h>" => error: cannot open source file
"stdint.h"
What I've tried:
I tried sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target / and it says "upgrade successful", but does not resolve any errors.
I tried uninstalling and reinstalling XCode (Version 10.1)
I tried downloading the .pkg file you mentioned (because I was missing it) but was told my macOS version was too new
The files I need are stored in /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include, so we added a new CPATH and PATH variable (separately) for that path to ~/.bash_profile and /etc/paths
I tried to provide #include statements with explicit paths to those files, which quickly spiraled out of control
I tried to move all of the required files from the above directory to my project folder and changing the paths appropriately in the code
I tried to run sudo ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
The files should also be stored in Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include, so I tried doing the above steps with this path as well
Any advice would be helpful. Thank you!
To provide more clarity based on a comment from Joshua below, these are the steps I followed to set up the 32-bit ARM cross-compiler:
sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /
brew install gmp
brew install mpfr
brew install libmpc
Downloaded "binutils-2.36.tar.xz" from here
Outside of the binutils directory, created a directory called
"build32"
../binutils-2.36/configure --target=arm-none-eabi --disable-nls --enable-languages=c --without-headers
make -j4 all
sudo make install
Downloaded "gcc-arm-src-snapshot-10.2-2020.11.tar.xz" from here
Inside the main directory, created a subdirectory called "build32"
../gcc-arm-src-snapshot-10.2-2020.11/configure --target=arm-none-eabi --disable-nls --enable-languages=c --without-headers
make -j4 all-gcc
sudo make install-gcc
I ended up switching to a Windows Subsystem for Linux setup on another machine for what I was working on. Another option is to set up a VM on the Mac. I could not figure out another solution for getting it to "just work" on the Mac. Thanks to everyone for the suggestions!
For those looking to pivot to WSL, follow this setup guide.
For those looking to go the VM workaround route, this is one process that was graciously recommended to me:
"1. Install Virtualbox from https://www.virtualbox.org/
Install Vagrant from https://www.vagrantup.com/
Download the prebuilt ARM gcc cross compiler from the link above in my question.
Move the download file into the project directory and run the following command to extract it:
tar -xvf gcc-arm-10.2-2020.11-x86_64-arm-none-eabi.tar.xz
Download this Vagrantfile (fyi 'this' triggers a download of said file) and move it to the project directory as
well. This file tells vagrant how to setup the VM. It will mount the
project directory as well as the SD card. On line 41 of this file it
tells vagrant where the SD card is located so change the first path
on this line to where your SD card is located. Additionally, because
the project directory is mounted, you can still edit the project
files locally on your machine instead of having to use what's on the
VM if you use this solution.
After all that, you should be ready to create/start the VM using the
command vagrant up. To log into the VM, vagrant ssh after the
vagrant up command finishes. Your project files should be located in
the /vagrant directory. You are now all setup to compile the project
and if you run make cp it should compile the project and copy it
over to the SD card.
To shutdown the VM you can run vagrant halt to shutdown the VM, and
to get rid of the VM you can run vagrant destroy. vagrant destroy
will not delete any of the files that were shared from your computer
to the VM."

How can I run GCC into WSL?

So, Windows System on Linux seems to be an inovative light-weight substitute for a virtual machine, especially when it comes to learning Linux. Nevertheless, for me WSL seems to have a little more complicated organization from a VM when it comes to file management. For example, in WSL the data are stored in
C:\Users\<username>\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState\rootfs\usr\
as shown here.
Of course my first try to install GCC was to type in:
sudo apt install gcc
,but after typing
gcc --version
the output was :
Command 'gcc' not found, but can be installed with:
sudo apt install gcc
After that, I tried installing GCC using the following commands as shown here :
$ sudo apt-get update && sudo apt-get upgrade -y
$ sudo apt autoremove -y
$ sudo apt-get install gcc -y
but I end up getting the same output.
On the other hand, I already had installed TDM GCC on Windows and used sudo in VM to install it also there (successfully:).
Like a lot of people, I want to substitute the use of my virtual machine with WSL in order to be able to develop in C without overloading my RAM my questions are :
Can you count out for me how many times have I downloaded GCC files?
Why the folder that I was supposed to be downloading the gcc files in is shown to be empty?
Where are the downloaded files being stored?
How can get gcc fired up?
Is there any way to use the gcc I had on Windows to compile C using the WSL terminal?
I think these questions come from the fact that I cannot understand how WSL file system interacts with Windows. They are a bit more generic on purpose because I want you to have a lot of "answering" freedom.
Please help me out of this one! I don't want to be forced to use a virtual machine and run out of RAM almost every time!

Compile Linux kernel on one machine (host) to be installed on another (VM)

I tried to compile the Linux kernel on the host machine to be installed in the virtual machine. The obvious gain here is performance.
I have been using the following steps after copying over the ".config" file from the VM:
$ export CONCURRENCY_LEVEL=9
$ fakeroot make-kpkg --initrd --append-to-version=-myext-0.1 kernel_image kernel_headers
However, I am noticing a behavior where the compilation/packaging packages symlinks for the header files rather than the header files themselves. This behavior is fine if I am installing the packages on the same machine. However, when I copy the packages to the VM and install them, all I have installed are symlinks to files that do not exist in the VM. They exist on the host machine.
Anyone knows how to fix this? I could be missing a flag or something of that sort.

Resources