LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-21-2018, 08:34 AM   #1
velvetmist
LQ Newbie
 
Registered: Feb 2018
Posts: 4

Rep: Reputation: Disabled
How to solve 'collect2: ld returned 1 exit status '?


Hi guys, today I wanted to start learning about programming, so I wanted to do that Hello World crap thinking it would be really easy, so I wrote the program:
Code:
#include<stdio.h>

#include<stdlib.h>

int main()

{

printf("\nHello World!");

return(0);

}
And for compiling it I wrote the command:
Code:
gcc -Wall -W -Werror helloworld.c -o helloworld
But it didn't work and I got:
Code:
collect2: ld returned 1 exit status
I googled for a solution but I didn't found anything, help me please, thx.
 
Old 02-21-2018, 08:51 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
if your code is correct in how it is being presented here,then add spaces between your include and file
Code:
#include <stdio.h>
not
#include<stdio.h>
I just tested that theoy and well make a lair out of myself?
Code:
#include<stdio.h>

int main(void)
{
    printf("hello wOlrD\n");
return 0;
}
Code:
bash-4.3# nano mainhello.c
bash-4.3# gcc mainhello.c
bash-4.3# ./a.out
hello wOlrD
without errors.

Quote:
The exit status error just signals that the linking step in the build process encountered some errors.
just compile it like I did with default output and see what that does for you, your program will be named a.out

c++
Code:
#include <iostream>

using namespace std;

int main()
{
  cout<<"HEllO World"<<endl;

return 0;
}
Code:
bash-4.3# nano mainhello.cpp
bash-4.3# g++ mainhello.cpp
bash-4.3# ./a.out
HEllO World
BASH
Code:
bash-4.3# cat >> myhello 
#!/bin/bash
echo "HELLOS WOrld"
^D

bash-4.3# ls myhello
myhello

bash-4.3# chmod +x myhello
bash-4.3# ./myhello
HELLOS WOrld
Excuse me for being confused, because I am not getting the same error. in any of the three, BASH will not return that error, I just thought I'd add that for you benefit to show you BASH too.

You could run ldconfig on your system to see update your libs.
http://man7.org/linux/man-pages/man8/ldconfig.8.html
But I'd just try gcc helloworld.c first

Last edited by BW-userx; 02-21-2018 at 09:13 AM.
 
Old 02-21-2018, 09:17 AM   #3
velvetmist
LQ Newbie
 
Registered: Feb 2018
Posts: 4

Original Poster
Rep: Reputation: Disabled
[QUOTE=BW-userx;5822380]
Code:
bash-4.3# gcc mainhello.c
I tried it but it didn't work
When I write that command I get the same error.
 
Old 02-21-2018, 09:45 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,699

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Quote:
collect2: ld returned 1 exit status
Is that the entire error/warning message displayed from the output of the gcc command? Please post the entire output.

As you have discovered the compiler does not care about spaces...
 
Old 02-21-2018, 10:06 AM   #5
velvetmist
LQ Newbie
 
Registered: Feb 2018
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
Is that the entire error/warning message displayed from the output of the gcc command? Please post the entire output.
Code:
/usr/bin/ld: cannot open output file helloworld: Permission denied
collect2: error: ld returned 1 exit status
I also tried with: https://askubuntu.com/questions/4666...mission-denied
First with:
Code:
sudo chown -R "$USER:" /path/to/the/directory
it didn't worked so i tried with:
Code:
chmod -R 700 /path/to/the/directory
and then when I tried to cd to that directory it says:
Code:
bash: cd: /path/to/the/directory: Permission denied
 
Old 02-21-2018, 10:15 AM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
where is it exacly? on your home/user why are you changing permissions?

755 read write execute user
Code:
Frequently used numeric parameters for chmod
755	The general preferred permissions for almost all the files on your disk
775	General files used when working as a Group (Others can only view/execute your files)
770	Important files used when working as a Group (Others cannot do anything with your files)
using -R changes every thing
Code:
echo $USER: <---- that colon is not suppose to be there
it screws it up
Code:
$ echo $USER:
userx:
userx@slackwhere101:~
$ echo $USER 
userx
there is no userx:
that might be it
Code:
userx@slackwhere101:~
$ touch permissions700
 
$ chmod 700 permissions700

$ ls -la permissions700
-rwx------ 1 userx users 23 Feb 21 10:22 permissions700

$ echo "in file permissions700" >> permissions700
 
$ cat permissions700
in file permissions700
when changing owner I usually add user group, but for whatever reasons you're just making it so no one but you can do anything to that dir and whats in it.
Code:
chown userx:users /path/to/dir -R
nevertheless, remove the : colon and redo it.

Last edited by BW-userx; 02-21-2018 at 10:28 AM.
 
Old 02-21-2018, 10:15 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,699

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Lets start from scratch.

What distribution/version are you running?

How did you create helloworld.c? What were the exact commands used. What directory is helloworld.c located.
 
Old 02-21-2018, 11:06 AM   #8
velvetmist
LQ Newbie
 
Registered: Feb 2018
Posts: 4

Original Poster
Rep: Reputation: Disabled
Well, I tried to change the permissions, I mean I wrote the command:
sudo chmod 777 /path/to/my/folder
but when I put:
ls -l path/to/my/folder
I got
-rwx------ 1 root rooot 100

Quote:
What distribution/version are you running?

How did you create helloworld.c? What were the exact commands used. What directory is helloworld.c located.
I've got Debian.

I first wrote
Code:
sudo nano helloworld.c
Then the program I told you above, and then I tried to compilated it.
 
Old 02-21-2018, 11:30 AM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by velvetmist View Post
Well, I tried to change the permissions, I mean I wrote the command:
sudo chmod 777 /path/to/my/folder
but when I put:
ls -l path/to/my/folder
I got
-rwx------ 1 root rooot 100



I've got Debian.

I first wrote
Code:
sudo nano helloworld.c
Then the program I told you above, and then I tried to compilated it.
mmm why are you root?
programming is not system admin. Nonetheless, if you are logged in as root, which I don't think you'd be because Debain hides root account.
and you are an user and want to use that dir
Code:
sudo chown user:group  /path/to/dir -R
#to fix it so you can use it as a user, 
the group is a group you belong to, mostly your main group. 
then,
as I am in root account at the moment.
Code:
root@slackwhere101:~
# login userx
Password: 
Linux 4.4.14.
No mail.

The problem with the gene pool is that there is no lifeguard.


userx@slackwhere101:~$ mkdir ~/testdir

userx@slackwhere101:~$ ls -la ~/testdir
total 8
drwxr-xr-x  2 userx users 4096 Feb 21 11:35 ./
drwx--x--x 34 userx users 4096 Feb 21 11:35 ../

userx@slackwhere101:~$ sudo mkdir ~/testdir2
userx@slackwhere101:~$ ls -la ~/testdir2
total 8
drwxr-xr-x  2 root  root  4096 Feb 21 11:35 ./
drwx--x--x 35 userx users 4096 Feb 21 11:35 ../

userx@slackwhere101:~$ sudo chown userx:users ~/testdir2 -R
userx@slackwhere101:~$ ls -la ~/testdir2
total 8
drwxr-xr-x  2 userx users 4096 Feb 21 11:35 ./
drwx--x--x 35 userx users 4096 Feb 21 11:35 ../

userx@slackwhere101:~$ chmod 700 ~/testdir2
userx@slackwhere101:~$ ls -la ~/testdir2
total 8
drwx------  2 userx users 4096 Feb 21 11:35 ./
drwx--x--x 35 userx users 4096 Feb 21 11:35 ../
if you look at the command sudo and creating a dir who gets ownership of it, and whoever has ownership of the dir or file can do whatever they want to it. even changes permissions on it, no need to use sudo if user owns the dir.

Last edited by BW-userx; 02-21-2018 at 11:44 AM.
 
Old 02-21-2018, 11:55 AM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,699

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
sudo allows a regular user elevated priveledges to run commands but it isn't necessarily for what you are doing.

As suggested run the chown command to change pemissions for your file.
 
Old 02-21-2018, 06:49 PM   #11
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
I am coming in here a little late in the game, so let me recap and offer my own suggestions in addition to the excellent comments already posted.

First, when posting error messages, especially compiler messages, always include any lines which indicate error, failure, missing files or permission problems, etc. Very often the final error message is the result of something already reported, such as the file permission problem in this case. That will save time and effort and help others to more quickly understand what is happening.

I will defer to others to help with fixing the bad file permissions if they like, but please note that this is a separate, self-inflicted problem not actually part of your initial interest in building the hello world program. That resulted from unnecessary use of sudo and chmod, not from any normal code or compile problem - so knowing that I might suggest that you simply start over, without sudo, write, compile and run your hello world program. Then try to learn about file permissions, sudo, etc. as a separate and better focused task.

And as I am sure you will hear from others - often - never, ever try to fix a problem like this...

Quote:
Originally Posted by velvetmist View Post
Well, I tried to change the permissions, I mean I wrote the command:
sudo chmod 777 /path/to/my/folder...
Doing so is always wrong, will mask other problems and dig yourself deeper into whatever hole you are stuck in at the moment!

Try to develop a reflexive "STOP!" response when told to chmod 777, and don't form that very bad habit! Instead, spend just a little time trying to understand the basic why and how of Unix/Linux file permissions. It is really pretty simple and well founded, and you will have to learn your way around it sooner or later - make it sooner!

A little time spent reading well written online references such as Rute User's Tutorial and Exposition, Bash Guide for Beginners, Advanced Linux Programming Book, and many others, will be well spent! And all of the will include some version of Hello World to help get you started!

Good luck!
 
Old 02-21-2018, 07:05 PM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,699

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Quote:
I might suggest that you simply start over, without sudo, write, compile and run your hello world program. Then try to learn about file permissions, sudo, etc. as a separate and better focused task.
Exactly...
 
Old 02-21-2018, 07:12 PM   #13
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
I have a bin and scripts in my home dir where I put my stuff in and sub dirs to try and keep it organized.
 
Old 02-21-2018, 11:31 PM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@OP Go back to square 1 (delete the previous files), but this time forget su, sudo, chmod and alike. All you need is text-editor and gcc. And if you get some error messages quote all of them, not only the last line.
[Edit: I see it has been suggested again and again. You should actually do it.]

Last edited by NevemTeve; 02-21-2018 at 11:39 PM.
 
Old 02-22-2018, 02:26 AM   #15
Delcaran
Member
 
Registered: Dec 2013
Location: ud.fvg.it
Distribution: Slackware64 14.2
Posts: 44

Rep: Reputation: 27
Bored at work so replying just for clarity. You should do as following:
  1. Delete everything you have done.
  2. As normal user, create a new directory.
  3. In that directory, create helloworld.c. Beware of the spaces after #include: spaces aren't normaly an issue in code, but #include is a preprocessor directive and is not exatcly code...
  4. Compile, link and execute.
  5. Get yourself a copy of the Kernighan & Ritchie, the Bible for C programming.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
collect2: ld returned 1 exit status salathia Linux - Newbie 1 06-27-2013 11:04 AM
[SOLVED] collect2: ld returned 1 exit status linuxpremi Linux - Newbie 3 01-30-2011 10:54 PM
A different collect2: ld returned 1 exit status old_as_a_fossil Linux - General 0 01-28-2010 09:57 AM
collect2: ld returned 1 exit status kvijaik Linux - Software 2 10-07-2009 09:17 PM
collect2: ld returned 1 exit status Ritendra Linux - Newbie 2 07-30-2009 07:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration