Thread: How to program in unix

  1. #1
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149

    How to program in unix

    Hi,

    I realize this is a forum for linux, but i figured someone here could probably help.
    I am taking a programming course at my college, and we have to make sure our programs run on a unix machine. I have no experience programming in unix and almost no experience using unix at all.
    I use putty to connect to the unix machine at our college. I only know how to program using Microsoft Visual Studio 2005. So, i was wondering if there is a way to create my program in Visual Studio, and somehow transfer it into unix to make it work there.

    This is what my college's machine is:
    The Sunfire v880 is a UNIX based computer running Sun's variant of UNIX called Solaris.

    I read somewhere that i have to have the GNU compiler. I typed "which gcc" into the command prompt and it said: "/user/local/bin/gcc" (i'm not sure what this means). So, i don't know if i have to install the gnu compiler (not sure how i would install it).

    Basically, i would like to program in Visual Studio, and transfer it into the unix machine to get it to work there.

    Any help would be appreciated.
    Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    which is a command that tells you, "If you typed that command at the prompt, this is the file that I would execute". So, gcc is installed (in the directory /usr/local/bin). No worries there.

    In order to make sure that your program will transfer successfully, you should make sure there's no Windows-specific things in your code. Make sure you create a console application. Don't use precompiled headers (if you see #include "stdafx.h", that's bad). Don't use anything from windows.h either. Anything that's "standard" (in other words, most things that are in your textbook) will work just fine. Eschew system() calls. We preach portable code pretty vigorously on the main boards, so you can ask questions about your code there -- unless you're actually writing OS-specific code.

    You'll have to FTP your files from one machine to the other, and sometimes even finding your source code in Visual Studio is not trivial. You'll also have to deal with line-ending issues; your source code on the UNIX machine will have "^M" at the end of every line. (Quick hack: type vim file.c (where file.c is your source), then :set fileformat=unix[return] then :wq[return]. Vim is not an editor for the faint of heart, but this is the quickest way I've seen to make this change (assuming vim is installed).)

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    If you FTP the files as text files, the line ending should be adjusted automatically. Use the 'ascii' command (within FTP) before transferring the file.

    Once copied across, for a simple console program, you would type
    gcc prog.c
    to compile, and

    ./a.out
    to run
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    I made a quick program to test:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
    	int array1 [4] [5];
    	int row;
    	int column;	
    
    	cout<<"Please enter twenty numbers "<<endl;
    
    	for(row=0;row<4;row++)
    	{
    		for(column=0;column<5;column++)
    		{
    			cin>>array1[row][column];
    		}
    	}
    	for(row=0;row<4;row++)
    	{
    		cout<<endl;
    		
    		for(column=0;column<5;column++)
    		{
    			cout<<setw(2)<<array1[row][column]<<' ';
    		}	
    	}
    	cout<<endl<<endl;
    }
    First off, would iostream and iomanip be considered as precompiled headers? If so, how would i make it work.
    The project name is "UnixTest", and the .ccp file name is "Test.cpp".
    How would i go about FTP'ing that to the unix machine. Would i use putty or some FTP program (never used a FTP program before)?
    And, would i save the .cpp file as a .txt file first?
    And, where in the unix machine would i transfer it to (specific directory?).

    I know i have alot of questions since i have very limited unix experience. I plan on getting some help from the instructor of my class, but i figured the more help the better.

    Thank you for all the help.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > First off, would iostream and iomanip be considered as precompiled headers?
    No, they're standard headers.

    The putty package also has psftp (which is the secure version of ftp, so it would work in the same way).

    > And, would i save the .cpp file as a .txt file first?
    No, leaving it as a .cpp would be fine.

    > And, where in the unix machine would i transfer it to (specific directory?).
    Anywhere you like (or more accurately, anywhere you have permission).
    When you log in via FTP (or PSFTP), you'll be set to some directory on the remote machine. Use the 'cd' command to set the remote directory to where you want the files to go. Depending on what the sysadmin has allowed, you may only be able to copy the files to a public area, and then you have to use a 'cp' command at your putty prompt to copy from that public area to your own user directory.

    Here's an example FTP session (PSFTP should be similar).
    Code:
    $ ftp
    ftp> open ftp.myhost.com
    Connected to ftp.myhost.com.
    220-PLEASE NOTE: If you have a non-subscription account you
    220-will only be able to login to this ftp server if you are
    220-dialled into our network.
    220-
    220-A Maximum of 4 concurrent connections are allowed to this
    220-server, each session is limited to 64KB/s downloads and
    220-32KB/s uploads.
    User (ftp.myhost.com:(none)): somebody
    331 Password required for somebody.
    Password:
    230 User somebody logged in.
    ftp> ls
    200 PORT command successful
    150 Opening ASCII mode data connection for file list
    htdocs
    logs
    226 Transfer complete.
    ftp: 14 bytes received in 0.00Seconds 14000.00Kbytes/sec.
    ftp> cd htdocs
    250 CWD command successful
    ftp> ascii
    200 Type set to A
    ftp> put foo.c
    200 PORT command successful
    150 Opening ASCII mode data connection for foo.c
    226 Transfer complete.
    ftp: 313 bytes sent in 0.00Seconds 313000.00Kbytes/sec.
    ftp> delete foo.c
    250 DELE command successful
    ftp> ls
    200 PORT command successful
    150 Opening ASCII mode data connection for file list
    images
    forsale
    wanted
    index.html
    freestuff
    226 Transfer complete.
    ftp: 48 bytes received in 0.02Seconds 2.40Kbytes/sec.
    ftp> close
    221 Goodbye.
    ftp> quit
    Also, you can use windows explorer if you want a nice GUI interface with a familiar drag and drop interface.
    Just type in
    ftp://ftp.myhost.com
    into the address bar, then it will prompt for your username/password. You should then be able to transfer files easily.

    When all that's done, then
    g++ test.cpp
    ./a.out

    should compile and run your program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Ok, i tried it using windows explorer and it came up with:
    "The folder "ftp://ftp.xxx.edu" is read-only because the proxy server is not set up to allow full access. To move,paste, rename, or delete files, you must use a different proxy."
    Nothing came up for me to log in. There were some files that came up, but none i recognized.

    So i tried using the ftp in putty.
    I got to (in your example):
    ftp> put foo.c
    Am i suppose to type:
    "put Test.cpp"
    I tried this and it said no such file or directory exisits.
    I made a folder called TestDir on the unix machine, so i typed "cd TestDir" on the ftp thing.
    Then i made it to the put command, and i couldn't upload my file from my computer to the unix machine. I also tried:
    "C:\Test.cpp"
    I couldn't get anything to work. It kept on saying no such file or directory exisits.

    Thanks

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    You need to find out from your university sys admin what kind of FTP access is allowed, what the username/password is going to be for you, etc.

    I just took the (sanitised) log of a transaction with my own ISP, so your mileage may differ.

    But I'm assuming since you got to the "put" command that you managed to login to your site's FTP server right?

    > "put Test.cpp"
    > I tried this and it said no such file or directory exisits.
    Yeah, you need the actual filename you want to transfer. See the FAQ on the true nature of 'foo'.
    Also, the "lcd" command allows you to change directory on the LOCAL machine, whereas the "cd" command changes directory on the REMOTE machine.

    Invoking FTP from the directory containing the files you want to transfer is another way.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Ok, i must be misunderstanding something. Here is what i got so far:
    Code:
    $ ftp
    ftp> open myhost
    Connected to myhost.
    220 csa FTP server ready.
    Name (myhost:name): name
    331 Password required for name.
    Password:
    230 User name logged in.
    Remote system type is UNIX.
    Using binary mode to transfer files.
    ftp> ls
    200 PORT command successful.
    150 Opening ASCII mode data connection for file list.
    TestDir
    public_html
    226 Transfer complete.
    22 bytes received in 0.016 seconds (1.32 Kbytes/s)
    ftp> cd TestDir
    250 CWD command successful.
    ftp> ascii
    200 Type set to A.
    ftp>
    So, i'm not sure what i type in next. The path of the file on my computer's desktop is:
    C:\Documents and Settings\User\Desktop\Test.cpp
    And i want to put the file "Test" from my desktop into the unix directory "TestDir".
    I tried this, but it didn't work.
    Code:
    ftp> put Test.cpp
    Test.cpp: No such file or directory
    I also tried
    Code:
    ftp> lcd C:\Documents and Settings\User\Desktop\
    usage: lcd local-directory
    and did the put command again, but it still didn't work. I'm not sure what i'm doing wrong.

    Am I correct by saying the LOCAL machine is my computer, and the REMOTE machine is the unix machine?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need quotes around paths that have spaces in them. You could of course simplify the process by keeping your file in some path that doesn't have a bunch of spaces in it!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    i tried it with quotes:
    Code:
    ftp> lcd "C:\Documents and Settings\User\Desktop\"
    C:\Documents and Settings\User\Desktop\: No such file or directory
    also tried
    "C:\"
    and it said the same thing.
    I also tried it on my other computer, same problem.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What output do you get for "lpwd"?

    It's a very long time since last time I used ftp on a PC, as I tend to use ssh (and putty on the PC) nowadays.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    I think putty supports some form of scp. That might be easier.

    Also, you could try opening a file on the unix machine in the putty console window (say, `emacs -nw Test.cpp`) and then copying and pasting your source into the window.

  13. #13
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Code:
    ftp> lpwd
    ?Invalid command
    I typed in help, and a list of commands came up. There was "pwd" but no "lpwd"

    I made a file with "emacs -nw " like you said and copied and pasted my code and it worked.

    Our program files (along with excel charts relating to the program) are to be handed in to/through the unix machine. The program's code could be copied and pasted fine, but i'm not so sure the excel charts could be.
    It would seem i would need to upload them from my computer to the unix machine. But i still can't figure it out. It seems like its not recognizing/finding my computer's files, or i'm just doing something wrong.

  14. #14
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    try putty's scp tool (pscp i think it's called). scp should work fine and you can transfer any file.

  15. #15
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    oh and by the way, speaking as a former TA for a course where many people used windows machines:
    make sure your code runs on the unix machines. Compile it, and run it before handing it in.

    You may need to convert line endings, there's a program on most unix boxes called `dos2unix` that will do it.

    Code:
    dos2unix Test.cpp > Test-unix.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. C Program to calculate the FILE Size Sytem in Unix
    By anwar_pat in forum C Programming
    Replies: 6
    Last Post: 02-23-2006, 10:17 AM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM