I started working with C on ubuntu and every time I want the code compile I have to type thee gcc command.

gcc hello.c -o hello -lm

I tried automating this with make on ~./bashrc, but it is not working when I have an additional library, such as <math.h>. Here is my alias command.

alias make $File = gcc -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused- 
                  variable -Wshadow $File.c -lcrypt -o $File -lm

Where $File is the name of the file without the .c extension. Does anyone know what I could change so it would include, say the math library? Also, is it possible that by chance the make command written by the staff is publicly available?


make exists already in Ubuntu (and most, if not all, Linux distributions). In order to use it, you need to create a makefile. You will see these files used in at least one of the later assignments and can read up more on making them from NTU.edu, from Swarthmore.edu, and fromGeeksForGeeks.com.

Simply put, though, you can put the commands needed to compile the program into a file called makefile (no file extension):

    gcc -o hello -hello.c -lm

and then run the make command in the directory with your program file and the makefile. This does not directly solve your compilation issue, but is how the command is already implemented and intended to be used. This is also a very simplified makefile and not one that I would use for anything other than these simple problem sets--not that I would use one for them over typing the command into the terminal.

Personally, I would not write an alias in my bashrc for this as you will not always need to include this library, nor will you always need to include only this library--meaning you will need to remember to change your alias (or write out the command) whenever your library dependencies change. Your dependencies will change from problem to problem and not all will be included in libm.

An easier option (in my opinion), and the one that I use, is to use the g++ compiler, which uses a lot of the same flags as gcc, but has increased functionality to work with C++. Using g++ hello.c -o hello will compile your program and include the math.h library. This has worked for the smaller-sized programs I have had to write for CS50 (though I did most of that work in the CS50 IDE for simplicity) and other courses, but most of my work is not in C or C++, so I have not had to include anything more than a handful or so of other libraries/files.

You could also download and build the clang utility from CS50. The instructions for this are in the CS50 Docs. This will let you use all of the same commands on your system that you would use in the CS50 IDE. To get this to work, you will need to set up some environment variables, which the docs do not specify. There is a post in the CS50 reddit about this, with some help from a user with the "staff" flag, but I have not done this myself. If you follow the thread, though, it lists the path variables that need to be added (which is helpful as not all of the links are still available).

Hope all of this helps. If so, feel free to click the check mark next to this answer. But, if not, please let me know and I will try to help further.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .