Run C programs from sublime 2.02 - c

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.

Related

Colored console output in Linux

I just started learning programming in C.
the first problem was to choose on which platform should I learn it, and I selected Ubuntu.
I found a GCC compiler to compile my projects, and it worked fine for me. I was running my compiled projects through Terminal.
But when I wanted to write a program which have to show a text on a colorful background, I understood that Terminal is not helping me. Actually I am learning from lessons written for programming on Windows, and they use there Borland C++ 5.2, but these lessons were written in 2007
So I am wondering, where I should run my compiled program in Ubuntu 10.10, to see the result properly?
Actually I tried to transfer to Windows, but Ubuntu on my computer works much faster, and it is easier I think to work in Ubuntu rather than Windows.
The simplest way to print color message without any toolkit is to use VT100 escape commands:
http://www.termsys.demon.co.uk/vtansi.htm#colors
So you can write something like this:
printf("\x1b[31mThis is red text\x1b[0m\n");
printf("\x1b[32mThis is green text\x1b[0m\n");
But this code works only for VT100-compatible terminals (almost all terminals of the Unix-system). The best way is to check TERM environment variable before colorizing your output.
Disclaimer: if you want to write code capable with all terminal types then you should use ncurses-like library.
The ncurses library does exactly what you are looking for. Using this library, you can choose to write any text, anywhere on the screen with any text/background color.
You can read more on it on Wikipedia. You can install it under ubuntu with:
$ sudo apt-get install libncurses5-dev
If you want to start with GUI programming in Linux, you will probably have to look into one of GUI toolkits, which is a library to write GUI elements, widgets, etc.
One of these toolkints is Gtk, which you can start to learn via Gtk tutorial. Another option is Qt which is probably easier to use, but that is based on C++.
I'm afraid that Borland C++ Windows courses are pretty much unusable for Linux GUI programming.
Or, if you just to want a colored terminal text, look into this question: stdlib and colored output in C

Programming via the terminal (Mac)

I have been using Textmate for writing my C source but want to do everything via the terminal.
How does one program via the terminal? Is anything else needed, installed?
In order to write source code on the Terminal, you will of course need an editor that works on the Terminal. In OS X, you have a lot of options to choose from. If you want simple and easy to use editors, you may want to try nano and pico that are available on OS X. If you start to want a little or more functionality out of the text editor, then you may want to try editors like vim and emacs, which are also available under OS X.
In order to compile your C source, you will require a C compiler. While OS X does not, by default, come pre-installed with a C compiler, you can quickly install a slew of development tools that are available in the OS X installation discs that you may have got when you bought your Mac. Those tools will provide you with gcc -- which is the GNU C compiler that you can use on the Terminal to compile your C sources --, gdb -- which is the GNU Debugger that you can further use to debug your compiled C source --, make -- which is GNU make, a program that, when you start to have to manage a number of C source files, can help you by creating make files and automating build operations -- and a number of other tools that you may want to explore as you spend more time with the system and become familiar with it.
You can use emacs or vi. vi is included in osx by default.
You can use a terminal text editor such as vim or emacs. You may also need to install gcc.
All these programs are available through any ports system.

what software should i use when i want to study c

i want to write code use notepad and compile the c code use cmd on windows (like java),
what software should i use ,
gcc for windows
cygwin
Turbo C
gvim
thanks
An excellent option for Windows is the Visual C++ Express Edition (which is free). This is a full IDE, compiler, and debugger and is a great way to get started.
GCC is also a good option if for some reason you wish to avoid an IDE. I wouldn't recommend this for a beginner, though.
Please don't use Turbo C, it's very old and you will have problems with it.
Cygwin is a popular linux environment for Windows.
Gvim is an editor and (in my opinion) has a very high learning curve. You could use Notepad++ or Code::Blocks to develop your code in.
mingw is also a good option to start with.
IMO you should avoid IDEs: a beginner needs to actually see and feel the whole process of compiling by invoking the compiler from a terminal, not by simply pressing some keys and bang! Also most of the IDEs have code completion facilities which is not good for beginners (especially for C). So I would go with vim (or gvim) and cygwin.
PS: Give Linux a try, you might actually like it; then vim and gcc.
Use cygwin. It's just about the only way to get a C environment on Windows that's both modern (i.e. not 16-bit, DOS-based) and conformant to the standards, and with it you get a fairly-complete POSIX environment, which allows you to actually do interesting things with C.
The classics would be Cygwin and MSYS/MinGW.
Cygwin provides a Linux API compatibility layer and includes a package manager which provides access to many well-known Linux applications (ie batteries included).
MSYS/MinGW only provides a minimal userland to get the GNU build system working.
Both come with their own shell environment, but can be used from Windows' command line as well.
Binaries created with Cygwin-gcc won't work without cygwin1.dll, but you get all the benefits of a mostly-POSIX environment. You can also use Cygwin's MinGW package for cross-compilation and thus create stand-alone binaries.
Other solutions are possible: Currently, I'm using the MinGW compiler together with the Cygwin userland from the Windows command line...
I think cygwin itself named as gcc for windows.
also gvim is not a compiler , It is a very nice and powerful tool for code editing.Also you can try emacs and kwrite
Some suggestions from my side:
In windows, you need to install a third party software to try all programming stuffs(c,c++ etc) ,Most of the times it is having GUI implementations and you didn't get any exact idea behind the code execution flow.also the command prompt of windows not at all efficient as linux shell.
But if you install a good linux distro (ubuntu/fedora/cent os/debian/pardus/suse/arch linux),all default compilers,debuggers and editors are installed within the OS and you can try the basic and all advanced programs by just opening the terminal(shell).Also the shell is a more powerful tool and you can enjoy all kind of automation comamnds(I mean scripting).
Also: for your added information, most of the embedded programmers in the industry prefer command line execution unlike GUI implementation because it is too fast than GUI.

non-IDE C development environment

How do I set up a non-IDE-based C development environment on Linux?
gcc + make + vi and you will live happily ever after!
A pretty good development environment is most likely already there--no setup necessary. This is because most Linux installations are GNU/Linux. GNU provides all the pieces you need for C software development.
Use the 'which' command to make sure these programs are in your execution path
which gcc
which make
Gcc is your compiler. Make lets you define the compiling and linking steps. (It can call gcc for you.)
You are likely to have several editors installed such as vi, nano, and GNU emacs. To see if you have emacs (for instance), type
which emacs
If the which command returns a valid path (i.e. it doesn't say which: no emacs in ...) then that means you can run the program just by typing its name:
emacs
for example.
Okay my environment:
Geany or gedit, make, GCC. If I have to code on the command line I'd probably go with vi or nano over emacs, I just can't cope with emacs anymore.
Okay, someone's gotta say it: Emacs, gcc, and make.
If - as a commenter suggested - you're looking for instructions on setting this up, there's probably no set up needed, as most Linux distributions I've used come with these programs already installed.
If you're not a masochist:
gcc + cmake + nano ;)
Make sure to edit your .nanorc file (you can copy an example from /etc/nanorc generally)
Emacs + Cedet is the best
I use gtkemacs, gcc, make but more often scons these days.
This question is dangerously close to inciting religious wars. Let me get it started: All configurations other than mine are inferior.
I would recommend gcc + vim/vi + (make).
Vim/vi is almost impossible to "just pick up and use" because it will be completely foreign and you will struggle to even move the cursor or insert text, but fortunately, Vim comes with an extremely helpful tutor you can start by invoking the command vimtutor in the shell. It will get you up and running in no time!
While Vim's interface is really bizarre and esoteric at first, once you get used to it (shouldn't take very long once you finish vimtutor), you will see your productivity when editing code increase dramatically!

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