LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   something wrong with gcc (https://www.linuxquestions.org/questions/linux-newbie-8/something-wrong-with-gcc-57241/)

freemind 04-28-2003 10:25 PM

something wrong with gcc
 
I made a simple C program (just printing out "Hello") to test gcc compiler in my newly installed Redhat 7.3 system. I type " gcc -o test test.cpp" and it compiled without any complains. Then I type "test" to run the program, but nothing shows up. I don't know what is wrong. Do I need to set the path for gcc even though it comes with the installation of the OS? Please advise, many thanks.

michaelk 04-29-2003 12:33 AM

Unless your program is in your current path enviroment you need to either type in the full path to the program or use the ./ shortcut.

In the directory where test is located:
./test

freemind 04-29-2003 10:05 AM

Thanks.

blinux1 04-29-2003 12:35 PM

you can also put it in a path directory like /bin or /sbin/

Mara 04-29-2003 02:22 PM

Or you can add the directory it's in to your PATH:
export PATH=$PATH:/somewhere

KDE4me 04-30-2003 05:18 AM

This might help you if the paths don't work (I had a similar problem)
[problems running c in bash]
http://www.linuxquestions.org/questi...threadid=57409

wapcaplet 04-30-2003 06:35 AM

I don't know if this is causing your problem, but you are compiling a C++ program (at least, according to the extension) with a C compiler (gcc). Use g++ instead.

shadowbird 04-30-2003 09:26 AM

Reference: Path statements.

One of the things I do is put "." as the first thing in my path. Thus, in my .bashrc file I have a statement:

export PATH=.:$PATH

freemind 04-30-2003 10:11 AM

Thank you all.

I tried ./test, it works. Here I have two more questions regarding your suggestions:

1. I want to export the Path in my .bashrc file, but I don't know what is its exact path. I use "locate gcc2.96", lots of messages come up. They are all in /usr/lib/....

2. I put "export PATH=.:$PATH" in my .bashrc file under the "#user specific aliases and functions" section. But it doesn't work. Did I do something wrong?

wapcaplet 04-30-2003 10:17 AM

If it's compiling correctly, then gcc probably already is in your path. An easy way to find out is:

which gcc

If it gives you a path to the executable, like /usr/bin/gcc, you're fine. If it says "no gcc found" or something like that, it's not in your path. Chances are, it is already in your path.

After you edit your .bashrc file, you need to restart bash. This means logging out and logging back in, if you're using terminal only, or closing and re-opening the terminal window if you're in a GUI. Or (I think this works too, haven't tried), type:

source .bashrc

shadowbird 04-30-2003 10:20 AM

Reference: having put "export PATH=.:$PATH" in your .bashrc file, did you then log out and log back in again? BASH only reads that file once, when it's started.

Secondly are you using BASH as your shell. If not, then changing .bashrc won't help you, and you'll have to change the proper configuration file for your shell :)

-- Kevin --

freemind 04-30-2003 10:46 AM

Hi Kelvin and Wapcaplet,

I tried your methods individually, and both don't work. I use "which gcc" and was given /usr/bin/gcc, then I put "export PATH=$PATH:/usr/bin/gcc" in .bashrc. Is it right?

After loging out and in and call "source .bashrc", problem is still there. Same with statement "export PATH=.:$PATH".

I think I'm using bash shell because if I type wrong commands,it gives me "bash:......"

Any idea for what is my problem? Really appreciate it.

shadowbird 04-30-2003 11:26 AM

eep! Nope! Not Right!

before you changed anything, you were set up correctly to run the COMPILER but not to run the resulting executable. In other words, typing in the command

gcc -o blah blah.c

would compile the program, but if you then typed in

blah

the program wasn't found. So, the path was correct to find the compiler, but was INCORRECT to find the executable.

here's what I'd like you to do:

1. Undo any edits you made to .bashrc because we told you to make them incorrectly... ::grin::

2. Find out which shell you're using by using the command:

echo $SHELL

if it comes out looking something like /bin/bash then you're using the BASH shell (which is good).

2. IF you're using the BASH shell, add the following command at the BOTTOM of your .bashrc:

export PATH=.:$PATH

(note that's PATH equals dot colon dollar PATH)

3. Log out and back in again. (to clear out any settings that have been made prior). using the source command will add to your path but won't undue any previous incorrect path statements.

4. type the command:

echo $PATH

in the resulting output, please find either "." at the beginning of the text OR ":.:" (that's colon dot colon) somewhere in the middle (an easy way of doing that is to do the following:

(echo $PATH | grep -q '\:\.\:') && echo found

if "found" appears then it's in there.

IF it's in there (either at the beginning or in the middle) then try the following:

gcc -o blah blah.c
blah

(replace "blah" with the name of your file)

This SHOULD work (it's how my system is set up here).

Here's a copy of my .bashrc:

# .bashrc

# User specific aliases and functions

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

export JAVA_HOME=/usr/java/sdk
export CATALINA_HOME=/usr/java/tomcat
export PATH=.:$PATH:/usr/local/ant/bin:/sbin:/usr/java/sdk/bin:


Note that the export statements are AT THE END.

-- Kevin --

freemind 04-30-2003 01:35 PM

Kevin, I did as what you mentioned above. Steps 1-4 are fine. But my programm is still NOT able to print out anything. When I
echo $PATH

I was given: .:.:.:/bin:/usr/bin/bin:/usr/bin/..............................(at the every beginning is "dot colon dot colon dot colon")

my .bashrc file is :

# .bashrc

# User specific aliases and functions

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

export PATH=.:$PATH

Do I need to do these steps as root?

shadowbird 04-30-2003 02:08 PM

Well, that's an excessive number of .:'s at the beginning, but that shouldn't hurt anything. When you execute the commands:

blah

(replace "blah" with the name of your program)

Do you get any error message, or do you just get the command prompt again?

If you just get the command prompt, then your program IS being run, it's just not outputting anything - can we see the source code to your program?

If you're getting an error message when you execute "blah", then please let us know what the message is.

Thanks!


All times are GMT -5. The time now is 10:07 AM.