Using make to compile code in c in terminal on Mac - c

I'm trying to learn to program in C and I'm getting used to compiling the code after writing it. I'm actually using K&R 2nd edition to start off and they use 'cc' to compile. I originally started using Zed Shaw's Learn C The Hard Way but I've stopped because I haven't heard many good things about it. Zed Shaw's way was to use 'make' to compile code, at least in the beginning.
Is there any technical difference between using 'make' and using 'cc' that I should be concerned about right now?

make simply gives commands to the compiler, such as cc. It automates building a project which has multiple files. To begin, you do not need to concern yourself with those details. Building a single file directly with cc is fine. In fact, you will understand how the compiler works so that you can get the most out of make when you eventually learn it.

I agree with code-apprentice. I would just add that it is important to have a good understanding of different cc command line options before moving on to make. When you reach the point of a more complexe compilation project and the build process of your final binary becomes a pain, that's when you can transition to learning Make!

Related

Create a final product through CodeBlocks with C Language

I am a newbie programmer (more specifically a student) which use CodeBlocks to many exercises (as much as I can). At the moment I am following this steps to create my own programs (File->New->Project->Console Application). This modality has served to learn C language, but I want create a software which any user could use (for now at Windows). Is it possible create some portable through various computers? I was exploring the another option I have on IDE but I don't understand so much, therefore I am very lost.
I googled about different projects which I can do with language C, but...sincerely, I got even more confused. Therefore, what's the next step, to make a more robust program in C?
Thanks
Make a HelloWorld program.
Run it in codeblocks.
Now go to the folder containing that file. You will find three files there.
HelloWorld.c
HelloWorld.o
HelloWorld
The last file HelloWorld is the executable. You can share it to other windows OS to run it.
EDIT:
C is a portable language so its viable. Do look into makefile as you intend to create a program that uses GUI, network etc. (RECOMMENDED)
Or you might want to create platform specific ones. For example, creating a debian package from your executable

switches in Triangle program

A weeks ago, I started learning about the Triangle program, which is used to generate meshes.
I attach the link from where you can download its zip file and read about it:
http://www.cs.cmu.edu/~quake/triangle.html
I am having trouble running the example using in Linux Ubuntu, which is supposed to be run trivially.
In particular, I don't understand what are exactly the switches that are mentioned in the makefile.
How exactly am I supposed to choose the optimization using switches? Do I need to modify the C source code or is something I need to specify before compiling it on Linux terminal somehow?
Besides, I am required to use the -DLINUX switches in order for arithmetics to run well. I would also need to know where I need to specify this.
If it helps, I am getting errors in the terminal notifying me that there are: "Undefined reference to sqrt" and " "Undefined reference to cos".
I really need help on this because my understanding on Linux and C is quite weak.
Thanks very much.

Getting error while run the program in C language

Getting warning each time while run the program in C.
This is not Error this is just a Warning .
Save the file every time before you Build and Run. This is the solution to your warning.This warning happens when you edit the program and tries to run it without saving.
Also your IDE Turbo C++ is very outdated Programming environment.I recommend CodeBlocks . Code::Blocks is a free C, C++ and Fortran IDE built to meet the most demanding needs of its users. It is designed to be very extensible and fully configurable.You can download it from here.
It seems like you are running this application in some sort of emulator.
Restart the program and try running that.
It should work.
Turbo C is terribly out of date and is probably even older than you.
I suggest you to upgrade to a IDE like Code::Blocks.
If, you are a beginner I suggest you compile using GCC command line compiler.
Save the file before you run/compile your code.

Run C programs from sublime 2.02

I want to compile and run C/C++ programs with sublimetext 2 itself.I dont want to use terminal to do so. What do I need to do for this??I am compeletely new to this so a lot of answers to similar questions did not help me as most of them were for windows.
sublime text 2 build system for C programming language should show you how to setup your build environment. Note: you need gcc etc. installed
Edit: you should although run your programm in a Terminal because SublimeText2 is an Editor and can not recieve user input for your programm. so you better just build it with ST2 and run it in Terminal or you just forget about ST2 and make your own Bash script that manages compiling and runs the programm.
I would use sublime text as an editor (which is what IDEs are). Then you'll need to have a Makefile for your program. And build it by typing make in a terminal (or by configuring your editor to run that command). BTW, you can type m a k e enter once in your day (and later use up in the same terminal, to reapeat the latest command).
Learning how to write simple Makefile-s is a reusable skill (see here for "why?"). See this and that and many other examples. Don't attach yourself to one particular editor (even if you may like one more than others; my preference goes to emacs).
You'll learn a big lot by studying (and improving) the source code of some free software. Look at sourceforge, github, etc.. to find some free software interesting to you, then download its source code, build it, and study it.
The tips and advices I gave here are still valid today, with a recent Ubuntu (e.g. 14.x)
BTW, your question looks like you might be confused; Sublime Text is an editor (perhaps glorified as an IDE). It does not compile anything. The C or C++ compiler on Ubuntu is GCC (using gcc command for compiling C code, and g++ command for compiling C++ code) or perhaps Clang/LLVM (using clang command for C code, and clang++ command for C++ code). That compiler is often started by make, and IDEs on Linux are often running make -or directly the compiler- command for you. You'll better be able to run these commands yourself.

How can I improve my C coding process on OS X?

I'm currently learning C from some Harvard screencasts. They're great. I'm currently, because I'm used to it, using this process for running my code:
Type it it TextMate
Save it
gcc filname
run "./a.out"
goto 1
Is there a major process, program, or best practice I'm missing here? I've tried apple-R in TextMate, but to compile something bare-bones like...
#include <stdio.h>
int
main(int argc, char * argv[])
{
int x = 342;
int y = 92;
int z = x + y;
printf("these values added are: %d\n", z);
return 0;
}
...takes 4 seconds which is clearly no good. Is there a great environment/compiler shortcut, or anything else I could be doing to make the actual testing more streamlined?
Thanks!
I use text mate and gcc in the terminal. I find Xcode too bulky for small commandline programs in C. It's great if you want to do cocoa GUI applications, need a quick Apple API reference or want a graphic frontend to gdb etc. 4 seconds sounds excessive for the program you have above, but it's probably due to Textmate cmd-R shortcut. I have never used that for anything else than scripts. But I agree that you should install Xcode, it's on your Mac OS install disk.
I disagree if you want to code ANSI C then this is what I'd suggest. Get familiar with a more powerful text editor, be it vim, emacs or whatever. I prefer vim.
See here:
http://macvim.org/OSX/index.php
Now, you should try to setup GNU Make for Mac OS X.
Doing this you will learn the methods most C programmers have used for ages. I think you'll get a lot more out of this than using Xcode straight out. With Xcode you want to make sure you are doing C and not Objective-C.
Now, with an editor like vim, you can do everything from a single terminal and waste fewer cycles switching around in your workflow.
So your workflow becomes:
$vim foo.c
(save)
$make
$./a.out
If you want to do any serious C programming that has any relevance to the UNIX envirnoment (and I suspect that Harvard course does), you will need to learn make at some point.
Install Apple's development tools, and use the XCode IDE. It may or may not increase your compile time, but it will greatly decrease much of the pain of hand coding and compiling on the command line.
To create a simple command-line C program (like the one you're describing above) do this:
File->New Project
Choose Command Line Tool as the project type.
The template includes a "hello world" program with a main.c file.
Edit the main.c file to include the code you're working on.
Run the project by choosing the Build and Run button.
**be sure to open the Console to view the output of your program. (Run->Console)
Good luck!
As mentioned before, XCode is the Mac way of doing it.
Just to give you an alternative solution, you can also use vi/vim from within a terminal. You open the file for the first time by typing vi filename.c. Then you type :w to save the file, :!gcc filename.c to compile it and :!./a.out to run it. The exclamation point does the trick of calling a shell command, so the last two commands can be replaced by one :!gcc filename.c; ./a.out . You also have a command history ("arrow up", "arrow down") from within vi, after pressing the colon.
Getting used to vi is a different story...
Try to build with gcc -Wall -Werror and try to understand and fix your warnings. That is always good practice.
I wouldn't recommended switching to XCode unless you need to use the debugger. GDB integration with XCode is pretty nice, and non-existent with Textmate.
To improve your Textmate workflow for smallish projects, learn to use make and install the Makefile bundle for Textmate. You can find it on GitHub.
This will give you a new Build ("Command-B") option in Textmate that will run make, parse the output, and let you easily correct compile time errors.
I think you should continue what you're doing. 4 seconds isn't that much time to wait, especially once your programs grow more extensive. Just using a basic text editor and gcc will let you master the basics of C. Once you're ready to start making GUI apps, then I would suggest to start using XCode to take advantage of the tools to make those kind of applications. I think an IDE is overkill at this point.
There should use Emacs or Vim with a Makefile. They can do anything.
For example, in Emacs, compiling is as simple as M-x Compile (But of course you could bind any key to it). Emacs is also cross-platform (Unlike XCode and TextMate).
Emacs also provides various other features useful for debugging (Built-in gdb support, ability to parse compile output, etc)
Using an IDE-Specific build tool is the exact wrong way to do things (Especially if that IDE happens to be platform specific, like XCode and TextMate)

Resources