|
I've exhausted my question for this entry, I'll bug u guys later :D /thread
So I'm doing the first example of the textbook now... I ran into trouble.
In Emacs yada.c: === #include <stdio.h>
main() { printf("hello, world\n"); } ===
In Terminal evan@Evan-Box:~/Documents/tryingout$ cc yada.c evan@Evan-Box:~/Documents/tryingout$ ls a.out yada.c yada.c~ evan@Evan-Box:~/Documents/tryingout$ a.out bash: a.out: command not found

nvm got it evan@Evan-Box:~/Documents/tryingout$ a.out bash: a.out: command not found evan@Evan-Box:~/Documents/tryingout$ ~/Documents/tryingout/a.out hello, world
So what did I do wrong? It seems I could type "emacs" in anywhere on the directory and emacs would run, but my a.out need to be specified by ~/Documents/tryingout/a.out for it to run... hmm
   
|
I'll be watching this blog constantly for the next 1 hour until I get answers xD
|
You got a Linux n stuff.... So now you can't play iCCup n stuff... You can't use Chaos Launcher n stuff... Poor guy I feel your pain. At least you won't have virus issues. People don't code viruses for Linux very often.
|
Why you had to get linux to do C programming I don't know, but ah well 
C is made in files that are basically .txt, but different extension (.h and .c). You compile those through gcc and link them and such, so that's what you'll be using mainly. Getting emacs can depend on your distro, but knowing Ubuntu, you probably already have it (I dunno though, never used Ubuntu). Someone more well-versed in C will have to help you with the rest
|
Thx Tec, my eecs roomate recommended getting a linux for that because he says linux has the more natural compiler and windows has a shit compiler or something haha. Hi Selbon, I use linux just to learn C, I still have windows.
|
I'm using Ubuntu and I find that emacs is not automatically installed. You can easily install it through "Add/Remove.." Applications.
|
GNU GCC complier not good enough?
|
On January 07 2009 16:34 IzzyCraft wrote: GNU GCC complier not good enough? :p I have no idea what are you talking about. I have 0 experience, please explain
|
United States3824 Posts
They made me get linux yesterday too at school for java. 
Here's what you do. Edit the program in a text file inbuntu should come with a text editor. Put the file in the folder that you want to compile in, and then get your compiler up and running. Basically you can do it with the command prompt if you navigate to the right folder and then use whatever C command to run your compiler. I would look in your compiler documentation for that though.
Or run it in windows and feel better about your life because you aren't a open source zealot.
|
United States3824 Posts
Wait if your roommate told you how to do it why didn't you ask him? Just saying.
|
United States3824 Posts
And furthermore isn't C a little archaic? Is this for a class or did you decide that you wanted to make yourself suffer by learning to program in a language that is really really effing difficult?
|
Out of curiosity, what do you plan on learning C for anyway? Depending on what you want to do, there may be a better language suited for your purposes.
|
Well if you're just learning how to program there is probably no better language then C imo and i do believe it's true native linux's support C much better then windows. Anyways what i mentioned is probably the most used compiler used for any C work unless professional. Which can always be done though cmd prompt like alot of things it just doesn't come native to windows.
|
thedeadhaji
39489 Posts
Kernighannn is an awesomee guuyyyyyyyyyyyy had him as faculty @ Pton
|
Like stated, you make text files with a text editor, doesn't matter if it's graphical or in the console. Then navigate the compiler to the file and compile it.
The extensions in Linux don't matter, while the permissions the files have - DO matter. I mean no matter what the file extension is, you can compile it with a C compiler as long as you have the permissions to do so.
|
K guys thanks. Is there anyway to find out if a certain application is on my system? Say I want to search for starcraft, how do I do that in the terminal? Now say I want to execute the program starcraft, how do that do that in the terminal?(Just type starcraft I guess? I typed emacs and it ran) thanks!
|
Alrite I just typed emacs in the terminal and it prompted me to install it. SO I guess I'll have emacs now. Now quesiton is: Where is emacs? It did not ask me where to put emacs and now I dunno where it is haha
All previous question still pertains.
|
thedeadhaji
39489 Posts
|
Germany2896 Posts
If you want to use win, you could get Visual Studio Express which might be a bit more familiar. Emacs, GDB, etc might be powerful, but they are quite different from what a windowser is used to. To play SC you need to install wine, as there is no linux native version of SC. And why do you want to do everything in the terminal? To manage apps there should be some packetmanager. But I don't know which one it is for your distri.
|
Hai, I know how to use emacs. I am not saying I want to play sc, I'm just using sc as an example of a program. I am asking how do I run a program from the terminal. So far my understanding is you just type the program.
For instance I jsut got emacs and to run it I just go yadayada$Emacs&
and it'll run it for me. Is my interpretation correct?
|
Not that linux isn't a fantastic os and emacs a fantastic powerful editor, but I think you should stick to windows while learning C (at least at first). I think you're trying to tackle too many things at once. I would pick one thing to focus on at a time, attempting to learn programming on an unfamiliar os will just slow you down. Not to mention how complicated emacs can be if you want it to be perfect for your use lol. I'd suggest using visual studio express or something first in windows. It'll let you focus more on the programming and will be a lot less overwhelming.
But, if you don't wanna heed my advice then uh Good luck!
|
On January 07 2009 18:34 Therapy wrote: Not that linux isn't a fantastic os and emacs a fantastic powerful editor, but I think you should stick to windows while learning C (at least at first). I think you're trying to tackle too many things at once. I would pick one thing to focus on at a time, attempting to learn programming on an unfamiliar os will just slow you down. Not to mention how complicated emacs can be if you want it to be perfect for your use lol. I'd suggest using visual studio express or something first in windows. It'll let you focus more on the programming and will be a lot less overwhelming.
But, if you don't wanna heed my advice then uh Good luck!
Everyone in my class, or will be in 20 days will be using linux coding in C. I don't think it's that hard or overwhelming. As I said, I'm not completely hopeless. Anyways I just wrote my first program and it printed hello world, awesome! Wish I know how to let it print it 10 times or more, wish I could write a program that could take an input x, that would print hello-world x times.
|
u have to use ./a.out in order to run a executable
|
Ah, if your class is using it then definitely stick with linux. Sounds like you're coping with the semi-foreign os fairly well (tho ubuntu is pretty nice and preconfigured for beginning users) Good luck with C, I'm a programming n00bie myself, just know a bit of c++ and php but not enough to really create anything of value. Although I've made my own lil forum system in php/mysql its fairly simplistic and boring 
EDIT: also,
On January 07 2009 18:42 evanthebouncy! wrote: Wish I know how to let it print it 10 times or more, wish I could write a program that could take an input x, that would print hello-world x times.
int num; printf ("Enter number of super awesome greetings you want: "); scanf ("%d",&num);
for(int i = 0; i < num; i++) printf ("Hello World! ");
should work fine, I'm not overly familiar with printf/scanf tho as i primarily use cout and cin for console programs (c++ stuff)
|
On January 07 2009 18:44 r3dox wrote: u have to use ./a.out in order to run a executable Hmm what does ./ do?
|
On January 07 2009 18:52 evanthebouncy! wrote:Show nested quote +On January 07 2009 18:44 r3dox wrote: u have to use ./a.out in order to run a executable Hmm what does ./ do? well in windows to run a program in console u just type the program name, but in bash on linux the ./ says you are trying to run something in the current directory
|
Aside from that it's been a long night, partitioning my laptop till now, I'm very happy. I'm going to bed. Thanks alot everyone. Gnite!
|
On January 07 2009 18:53 Therapy wrote:Show nested quote +On January 07 2009 18:52 evanthebouncy! wrote:On January 07 2009 18:44 r3dox wrote: u have to use ./a.out in order to run a executable Hmm what does ./ do? well in windows to run a program in console u just type the program name, but in bash on linux the ./ says you are trying to run something in the current directory Then how come emacs work in every directory?
|
Because its registered as an appliation in linux. Whereas programs you create or things not yet installed will need to be run using ./ if you wish to run them from current direction
|
Evan, are you Comp Sci? I thought you were something in social sciences, because your family is full of engineers and you wanted to c-c-c-combo break?
I assume you are not Comp Sci. I'm always excited to see people take up hobby programming! It is sooo enabling.
Let's see... I bought that same C book, read a few pages, and never opened it again. Yucky.
Ahh shit nevermind. I went over and grabbed this book from my boxes and am looking through it. It's not bad at all. I think I was thinking of a C++ book I bought that was written by the C++ author (yep I checked, I was thinking of Bjarne Stroustrup's The C++ Programming Language).
HERE WE GO
--------------------------------------------- DISCLAIMER: I AM NOT A LINUX EXPERT, BUT I USED IT THROUGHOUT COLLEGE. I HAVE A BS IN COMP SCI ---------------------------------------------
Let's start with your sexy Ubuntu environment. First thing, as a previous poster mentioned, make sure you have the incredible gcc ("GNU Compiler Collection") program installed. Basically, this all-in-one awesomeness will turn a simple text file into an executable program (it "compiles" your hand-written program). An executable on Unix/Linux is suffixed as ".out" whereas on Windows it is ".exe". You should expect gcc to spit out "a.out" (by default; this can be overriden) for any program you compile on Unix/Linux.
gcc works for many languages (thus, "all-in-one"). From their page:
The GNU Compiler Collection includes front ends for C, C++, Objective-C, Fortran, Java, and Ada, as well as libraries for these languages.
In other words, you can focus on the programming (fun stuff) instead of nasty library and dependency mangling.
(I won't go into details about the syntax of these commands, but you should read up on sudo and apt-get because this is essential Ubuntu/Linux stuff).
1) Update your Ubuntu installation files and install core utilities, including gcc - open a terminal and enter these commands, one by one: sudo apt-get update sudo apt-get install build-essential
2) Choose a Linux text editor. My favorite is vim ("Vi Improved"), which is a "modal text editor". It is an extended version of vi, an old school Unix editor. My alma mater, UCSD, has a good introduction to vi and a brief addendum for vim.
This is the first challenge of programming. Learning how to use a REAL text editor! (the other "classic" alternative is emacs which some of my friends use, but I genuinely hate it. emacs is an editor. emacs is an operating system. emacs is a way of life. emacs is overkill).
Spend maybe 1 hour learning vim and you're set for life. It enables you to dominate text files without reaching for the mouse (gvim is a "cheater version" of vim that let's you click with mouse. Defeats the purpose, IMO).
Customizing vim is not necessary but very helpful. You can put default settings in your home directory. Just create a file named .vimrc in your home directory (cd ~) and put settings there. Here is my simple, conventional .vimrc file:
+ Show Spoiler + " HeadBangaa's .vimrc " " 10/2008 -- for Cygwin VIM
" fix backspace, and make everything pretty set backspace=2 set syntax=color syntax on colorscheme pablo
" set search behavior, ruler, and line numbers set showmatch set ignorecase set ruler set nu
" use real tabs of length=4 set softtabstop=4 set shiftwidth=4 set smartindent
" set screen size set lines=52 set columns=80 set textwidth=80
3) Open your The [ANSI] C Programming Language (2nd Ed.) book and read pages 5-6 Ok this is a bit lazy of me, but there's no better start, really. To make sure your compiler and operating system are getting along, this walks you through the trivial "Hello, World!" program. One slight alteration: use gcc instead of cc ("cc" is a Unix dinosaur; Linux redirects "cc" to "gcc" anyways.)
If you're using vim as recommended: cd ~ mkdir HelloWorld ; cd HelloWorld vim hello.c This will create "hello.c" in a subdirectory of your home directory.
I suggest reading the rest of Chapter 1 as well!
4) ......
You've entered the world of C/Linux programming!! YOU FUCKING GEEK~!
Got any cool program ideas?
|
Ooops, your blog grew while I was typing all this. DON'T USE EMACS EVAN, NOOOOO!!!
looool my entire post is OBSOLETE. You don't have zero experience!!! LIAR!!!
|
For the initial problem, after compiling you want to do a chmod +x a.out, which sets its executable bit. Then you can run it with ./a.out.
Also, regarding the path issue, the /bin directory contains the most common binaries (or executables, as it were) of software currently on the system. /bin is in the path environment variable of every user, thus you can run emacs from any directory by simply typing emacs because your shell looks for a binary called "emacs" in every directory within your current path variable.
Right, I'm sleepy, I read some more posts. Therapy's code for even more Hello Worlds is right.
To search for files/directories from the command line there's the ancient locate tool. You need to run sudo updatedb or sudo locate -u to force a database update, wait a bit until it finishes indexing everything new, then use locate <search query>.
New programs are mostly installed in the same place, /usr subdirectories (most software) or /home subdirectories (e.g. your /home/<username> one, the default if you're prompted for an install path or if you're compiling the program from source). The /bin (and, respectively, /sbin) directories actually contain symlinks - think Windows-style shortcuts - to the actual files, with the exact same name.
Hope this makes sense to you. Have fun
|
You can't write "a.out" and expect Linux to find the file in the current directory because the current directory is never set in $PATH by default. In Windows, it is. But that is considered a small security risk because you might accidentally execute a file in the current directory which you didn't want to execute. So by default only the stuff in the "bin" directories (/bin /usr/bin /usr/local/bin) is set in $PATH and so the system finds it when you just type the name without a full or relative path in front. If you want to have the same behavior as in Windows, do a export PATH=$PATH:. (notice the dot at the end - it stands for current directory, which will thus be appended to the already existing PATH variable). This will just last for the current shell session though - to make it permanent add it to your ~/.bash_profile (or ~/.bashrc if it is executed) - I'm not going into detail on these files, just know that they are automatically executed when you log in (at a shell), so it's some sort of Autorun for the shell.
Executables in Linux typically don't have any file extension (think of /bin/ls, /bin/bash etc.) but you could name them however you want to - even ".exe" although that would be confusing because it would not be executable on Windows (because both systems have a different format for executables). gcc names an executable a.out by default but you can change that by just writing e.g. gcc -o exename src.c Recommendation: use no extension or ".bin"
|
This is the first challenge of programming. Learning how to use a REAL text editor! (the other "classic" alternative is emacs which some of my friends use, but I genuinely hate it. emacs is an editor. emacs is an operating system. emacs is a way of life. emacs is overkill).
The real reason vi is better than emacs: it was written by someone at UC Berkeley.... and you go to UC Berkeley. hehe.
But in all seriousness, I like to use it because it doesn't involve using the mouse at all. You just gotta figure out all the little keys and commands that let you do stuff like search/replace, cut/paste, etc.
I picked up vi by just knowing the most basic commands (i to insert, hjkl to navigate when not in insert mode, :w to save, :q to quit, :wq to save and quit, :q! to quit without saving, u to undo, and spamming esc to get out of whatever mode you were in previously), and then if I had to do something more complicated, looking up a hack on google until I memorized how to do it. Basically the list I spelled out in the ( ) is like 90% of what you use in vi.
Edit: Also useful for when you code, is creating an .exrc file (with vi as well lol), and in that file type these things:
:set number :set autoindent
|
If you want your executable to run from anywhere, you need to place it in /bin or /usr/bin
this is obviously not advisable for your own compiled code so yeah unless you declared your own path environ, you need to invoke the binary with a ./ in the front in the current directory.
like others have said, you should use gcc and gdb to compile and debug your programs.
do man gcc or info gcc...
expect to read through a lot of man pages blah blah to geta hang of the common tools.
I would suggest you head down to O'Reily 's site and have a look browse through their linux PDFs (Most of their books are open sourced so you can download and read it as a pdf)
But honestly, this is all very much info overload for some one starting out.
|
Oh yeah, stick with Linux It is so much easier (and free) to obtain tools to learn programming.
VI or VIM is so much superior to emac in just about any way possible but apple is apple, orange is orange.
|
On January 07 2009 22:18 haduken wrote: VI or VIM is so much superior to emac in just about any way possible but apple is apple, orange is orange.
Uhm, no? :p Both editors are amazingly powerful, and both are also amazingly weird for someone who is used to Windows or Mac GUI editors. Decide for yourself whether it's worth learning one of them - if you already know a bit about Emacs, just stick with it. There's nothing wrong with one or the other except from a personal preference perspective. If you want a more Windows/Mac like editor, I can recommend JEdit 4.3+, or Eclipse as an IDE.
|
Also, emacs key bindings are available for every editor that exists. Eclipse, etc. Still, use Vi!
|
On January 07 2009 16:29 evanthebouncy! wrote: Thx Tec, my eecs roomate recommended getting a linux for that because he says linux has the more natural compiler and windows has a shit compiler or something haha. Hi Selbon, I use linux just to learn C, I still have windows.
The Visual Studios compiler is absolutely fine :|
|
lolol... I've never seen so much bad/wrong/completely offtopic responses in one thread
your original question has already been answered, but to summarize very quickly: in linux, the command you type has to be built-in command ("cd", "ls", etc.) or a program in one of your executable folders which is specified by the PATH stuff (most likely "emacs", and tons of other common stuff "top", "less", etc. can be run just by typing their name)
"./" simply specifies the current folder you're in. you need need to specify a location to run programs not in the default executable folders (or you can add it to your PATH like people have mentioned though I don't recommend it).
basically, you don't need extra permissions to edit stuff in your personal folders, so you can't accidentally run a program and screw yourself over, and the "./" also means that you know you're running a program that's not in the executable folders
|
On January 08 2009 02:17 azndsh wrote: lolol... I've never seen so much bad/wrong/completely offtopic responses in one thread
your original question has already been answered, but to summarize very quickly: in linux, the command you type has to be built-in command ("cd", "ls", etc.) or a program in one of your executable folders which is specified by the PATH stuff (most likely "emacs", and tons of other common stuff "top", "less", etc. can be run just by typing their name)
"./" simply specifies the current folder you're in. you need need to specify a location to run programs not in the default executable folders (or you can add it to your PATH like people have mentioned though I don't recommend it).
basically, you don't need extra permissions to edit stuff in your personal folders, so you can't accidentally run a program and screw yourself over, and the "./" also means that you know you're running a program that's not in the executable folders
^This.
If you type this: echo $PATH
It will tell you what your current PATH variable is set to, which will be a list of a bunch of directories. This means that the shell looks for executables in those directories. Now, your a.out file is not in one of those directories - therefore it can't just run by command.
If you added your folder to the PATH variable (google that), you would be able to run an executable without specifying the entire path.
|
|
|
|
|