LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   error: ld returned 1 exit status (https://www.linuxquestions.org/questions/programming-9/error-ld-returned-1-exit-status-4175638005/)

rahulvishwakarma 09-08-2018 07:24 AM

error: ld returned 1 exit status
 
sub :- error: ld returned 1 exit status

hi to all, I've centos7.5 and codeblocks17.12 in VM. I am building a simple shared library file. but having following problem:-

header file:-
-------------
singlyll.h

Code:

#ifndef SINGLYLL_H_INCLUDED
#define SINGLYLL_H_INCLUDED

#include <iostream>
#include <cstdlib>

class  linklist
{
    struct node
    {
        int data;
        node *link;
    };

    node *start, *p;


public:

void display( linklist * pll );
};

#endif // SINGLYLL_H_INCLUDED

implementation file :-
---------------------
singlyll.cpp

Code:

#include "singlyll.h"

using namespace std;

void linklist::display(  linklist *pll)
{
    if(pll->start == NULL)
    {
        cout << "list is empty\n";

        return;
    }

    cout << "List : ";

    node *p = pll->start;

    for(; p != NULL; p = p->link)
    {
        cout << p->data <<"\t";
    }
    cout <<"\n\n";
}



build message :-
Code:

error: ld returned 1 exit status
-------------- Build: Debug in singlyll (compiler: GNU GCC Compiler)---------------
Code:

g++ -shared  obj/Debug/singlyll.o  -o bin/Debug/libsinglyll.so 
/usr/bin/ld: obj/Debug/singlyll.o: relocation R_X86_64_32 against `.rodata' 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), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))


BW-userx 09-08-2018 07:58 AM

your second line gives you a BIG Clue
Code:

/usr/bin/ld: obj/Debug/singlyll.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
your linking is wrong. look at the tail end of that clue. ;)

vector why not use that instead. just an idea.

rahulvishwakarma 09-09-2018 12:16 AM

i didn't get your point. please tell me how to do it in CodeBlocks.

Quote:

your second line gives you a BIG Clue
Code:
Code:

/usr/bin/ld: obj/Debug/singlyll.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC


NevemTeve 09-09-2018 03:56 AM

Compiler Settings
Other Options
add this: -fPIC

rahulvishwakarma 09-11-2018 07:53 AM

1 Attachment(s)
Thanks for reply. I did it like this :-
Attachment 28574

but having same problem.

BW-userx 09-11-2018 08:23 AM

what are you doing exactly? I did this,
link.h
Code:

#ifndef LINKKY_H_INCLUDED
#define LINKKY_H_INCLUDED

struct node
  {
    int data;
    node *next;
  };

class list
  {
    private:
    node *head, *tail;
    public:
    list()
    {
      head=NULL;
      tail=NULL;
    }
    void display(list * pll);
    void display();
  };



#endif // LINKKY_H_INCLUDED

link.cpp
Code:

#include<iostream>
#include <cstdio>
#include <cstdlib>
#include "linkky.h"


using namespace std;

void list::display(  list *pll)
{
    if(pll->head == NULL)
    {
        cout << "list is empty\n";

        return;
    }

    cout << "List : ";

    node *p = pll->head;

    for(; p != NULL; p = p->next)
    {
        cout << p->data <<"\t";
    }
    cout <<"\n\n";
}

void list::display()
  {
    node *temp=new node;
    temp=head;
    while(temp!=NULL)
    {
      cout<<temp->data<<"\t";
      temp=temp->next;
    }
  }

in codeblocks under settings->compiler-> I selected follow c++ 14 and it compiles without error.

i found this on a very similar error you're getting.

https://stackoverflow.com/questions/...str1-8-can-not

NevemTeve 09-11-2018 08:32 AM

You might want to do a 'Build All' or 'Rebuild' or 'Clean and Build' or anyhow it is called.

rahulvishwakarma 09-12-2018 06:35 AM

thank you very much friend,. I did as you told and I got solution.


All times are GMT -5. The time now is 03:09 AM.