Author Topic: error: ld returned 1 exit status  (Read 20831 times)

Offline Rahul

  • Multiple posting newcomer
  • *
  • Posts: 25
error: ld returned 1 exit status
« on: September 04, 2018, 12:06:29 pm »

hi to all , I've  centos 7.5 in VM. I m trying to compile a shared library file. But hanging up with message:-

Code
-------------- Build: Release in singlyll (compiler: GNU GCC Compiler)---------------

gcc -Wall -O2  -c "/opt/projects/code blocks/data structures/linkedlist/soproject/singlyll/singlyll/singlyll.c" -o obj/Release/singlyll.o
g++ -shared  obj/Release/singlyll.o  -o bin/Release/liblibsinglyll.so -s 
/usr/bin/ld: obj/Release/singlyll.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 1 second(s))
1 error(s), 0 warning(s) (0 minute(s), 1 second(s))
Code
||=== Build: Release in singlyll (compiler: GNU GCC Compiler) ===|
error: ld returned 1 exit status

singlyll.h :-
--------------
Code
#ifndef SINGLYLL_H_INCLUDED
#define SINGLYLL_H_INCLUDED

#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
    int data;
    struct node * link;
}node;

node *start, *p;

void display( struct node * start);
void search( struct node *start, int data);
 struct node * addatbegin( struct node *start, int data);
 struct node * addatend( struct node*start, int data);
 struct node * addafter( struct node * start, int data, int item);
 struct node * addberfore( struct node * start, int data, int item);

 struct node * createlist( struct node * start, int n);

 struct node * del( struct node* start, int data);
 struct node * reverse( struct node *start);

#endif // SINGLYLL_H_INCLUDED

singlyll.c :-
--------------
Code
#include "singlyll.h"

//struct
//{
//    int data;
//    struct node * link;
//}node;

void display(struct node* start)
{
    struct node *p;
    if(start == NULL)
    {
        printf("list is empty\n");

        return;
    }

    printf("List : ");

    for(p = start; p != NULL; p = p->link)
    {
        printf("%d \t", p->data);
    }
    printf("\n\n");
}

void search(struct node* start, int item)
{
    struct node* p = start;

    int pos = 1;

    for(;p != NULL; p = p->link)
    {
        if(p->data == item )
        {
            printf( "item %d found at position %d \n", item, pos);
        }
        pos++;
    }

    printf("item %d not found in list\n", item);
}

struct node* addatbegin( struct node* start, int item)
{
    struct node*temp  = (struct node*) malloc(sizeof(node));

    temp->data = item;

    temp->link = start;

    start = temp;

    return start;

}

struct node* addatend(struct node* start, int item)
{
    struct node* p = start;
    struct node* temp = start;

    temp = (struct node*) malloc(sizeof(node));
    temp->link = NULL;

    while(p->link!= NULL)
         p = p->link;
    p->link  = temp;

    return start;
}

struct node* addafter(struct node* start, int item, int data)
{
    struct node*p = start, *temp;

    while( p != NULL)
    {
        if(p->data == item)
        {
            temp = (node*) malloc(sizeof(node));
            temp->data = item;
            temp->link = p->link;
            p->link = temp;

            return start;
        }

        p = p->link;
    }

    printf(" not present in the list\n");
    return start;
}

struct node* addberfore(struct node* start, int data, int item)
{
    struct node*temp, *p;

    if(start == NULL)
    {
        printf("list is empty\n");
        return start;
    }

    /*insert before the first struct node*/

    if(item == start->data)
    {
        temp = (node*) malloc(sizeof(node));
        temp->data = data;
        temp->link = start;
        start = temp;

    }

     p = start;

     while(p->link != NULL)
     {
         if(p->link->data == item)
         {
            temp = (node*)malloc(sizeof(node));
            temp->data = data;

            temp->link = p->link;

            p->link = temp->link;

            return start;
         }

         p = p->link;
     }


     printf("%d not present in the list\n", item);
     return start;
}

 struct node* createlist(struct node* start, int n)
 {
    int i, data;

    start = NULL;

    if( n == 0)
    {
        return start;
    }

    printf("Enter elements to be inserted : ");

    scanf("%d", &data);

    start = addatbegin(start, data);

    for( i = 2; i <= n; i++ )
    {
        printf("Element to be inserted : ");
        scanf("%d",&data);

        start = addatend(start, data);
    }

    return start;
 }
struct node* del(struct node* start, int data)
{
    struct node*p, *temp;

    if(start == NULL)
    {
        printf("The list is empty \n");
        return start;
    }

    if(start->data == data)
    {
        temp= start;
        start = start->link;
        free(temp);

        return start;
    }

    p = start;

    while(p->link != NULL)
    {
        if( p->link->data == data)
        {
            temp = p->link;
            p->link = temp->link;
            free(temp);

            return start;
        }
        p = p->link;
    }

    printf("Element %d not found\n", data);

    return start;
}

struct node* reverse(struct node* start)
{
    struct node*prev, *ptr, *next;


    prev = NULL;
    ptr = start;

    while(ptr->link != NULL)
    {
        next = ptr->link;
        ptr->link = prev;
        prev = ptr;
        ptr = next;
    }

    return start;
}

have any one solution for this. how to handle this "ld returned i exit status" problem.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: error: ld returned 1 exit status
« Reply #1 on: September 04, 2018, 01:16:16 pm »
Code
/usr/bin/ld: obj/Release/singlyll.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Lenny17

  • Single posting newcomer
  • *
  • Posts: 1
Re: error: ld returned 1 exit status
« Reply #2 on: July 30, 2023, 06:59:20 pm »
I'm new to C programming and new to codeblocks - however but I did solve the problem for me over the hellow world; greetings beginners programs. I also got error id returned 1 exit status.

I found that the problem is caused if you exit codeblocks without closing files, projects, workspaces.
When re-opening codeblocks - it tends to default to the old workspace.
Now if you have run the hellow world program - it creates  code main.c
This is retained even in a new project.

I suggest when you create new projects - check that there is no residual coding blocks. Use the management console to check "source". Delete residual code blocks, add a new file to the new project and then try to compile.
Doing this allowed me to create a second project that gave the "greetings human" program!

Hope this helps - not seen a useful solution in a day of hunting the web.


MichaelAgarkov

  • Guest
Re: error: ld returned 1 exit status
« Reply #3 on: September 12, 2023, 08:21:22 am »
I also had the same error when compiling Hello World on Linux Mint.