undefined reference to shared object function

hi to all , i've codeblocks 20 in centos 7. I am building a small project in which i build a libdate.so file and called in my project. as like this :-
dtmanip.h :-
------------

#ifndef DTMANIP_H_INCLUDED
#define DTMANIP_H_INCLUDED

#include <string>
#include <bits/stdc++.h>


class Date
{
public:
void formDate(std::string &date, int y, int m, int d);
void splitDate(std::string date, int &y, int &m, int &d);
bool isValid(std::string date);
bool isLeap(int y);

private:
int y, m, d;
};

#endif // DTMANIP_H_INCLUDED

in file datefmt.cpp :-
----------------------

#include "dtmanip.h"

using std::string;

void Date::formDate(string &date, int y, int m, int d)
{
string strdt[9] = {"01", "02", "03", "04", "05", "06", "07", "08", "09"};

if(y < 1900 || y > 2100)
{
return;
}
else
{
date += std::to_string(y);
}

date += "/";
if(m < 10)
{
date += strdt[m-1];
}
else if( m > 9 && m< 13)
{
date += std::to_string(m);
}

date += "/";

if(d < 10 )
{
date = strdt[d - 1];

}
else if(d > 9 && d < 32)
{
date += std::to_string(d);
}
}

void Date::splitDate(string date, int &y, int &m, int &d)
{
string yy = date.substr(0, 3);
string mm = date.substr(5, 6);
string dd = date.substr(8, 9);

y = stoi(yy);
m = stoi(mm);
d = stoi(dd);
}

bool Date::isValid(string date)
{

return true;
}

bool Date::isLeap(int y)
{
return true;
}

now file libdate.so has been created successfully.. i copied and pasted header file in /usr/include and lib file in /usr/lib64

now i added this file in my project

in my project i declared object of class Date as *dt in header file and defined in constructor.
now when i called dt->splitDate(strdt, y, m, d);
here is error : classBill.cpp|584|undefined reference to `Date::splitDate(std::string, int&, int&, int&)'|

code is :

void classBill::todaySales()
{
mysql = classConn::connection();
mysql->reconnect = true;

draw->clrscr();
draw->drawRect();

draw->gotoxy(15, 3);

cout << "Sales By Date";
draw->gotoxy(13,4);
cout << "-----------------";

draw->gotoxy(10, 5);
cout << "Enter Date to see sale : ( yyyy / MM/ dd ) : ";


std::string strdt;

// dt->formDate(strdt, )

getline(cin,strdate);

int y, m, d;
dt->splitDate(strdate, y, m, d); // here is error

draw->gotoxy(10, 6);
cout << "date : " << y <<"/" << m << "/" << d;
getc->getch();
}
Last edited on
Please format your code using the Code format tags.

You can check what's exported from your shared library with nm. nm dumps pretty much everything, but exported symbols are described with T.

So to see all the exported symbols in your libdate.so, run:
 
nm -C libdate.so | grep " T "

If splitDate is missing, it didn't make it into the library. If it is present in the library, it's a build problem, and we'll need to see your link line.
i ran above command and found following o/p :-

[root@centos7client Debug]# nm -C libdate.so | grep " T "
00000000000026a0 T _fini
0000000000001650 T _init
00000000000020ca T Date::isLeap(int)
00000000000020b6 T Date::isValid(std::string)
0000000000001a06 T Date::formDate(std::string&, int, int, int)
0000000000001f2c T Date::splitDate(std::string, int&, int&, int&)

here splitDAte is not missing
Last edited on
> now file libdate.so has been created successfully..
> i copied and pasted header file in /usr/include and lib file in /usr/lib64
You need to tell the compiler (via Project->Build Options...) that you want to use that library.
Dialog should look something like https://imgur.com/a/YmNpy1C

Also, you shouldn't be putting your own headers and libraries directly in the system directories.
Use the search directories tab to list other places to search for include and library files.
salem c wrote:
Also, you shouldn't be putting your own headers and libraries directly in the system directories.
Use the search directories tab to list other places to search for include and library files.

This. Very much this.

You can have your custom libraries within you home directory and tell you IDE/build system/compiler where they are when you build dependent projects.


System directories have files installed from packages by package manager. Packages include metadata, so package manager knows what files belong to package and can later cleanly uninstall (i.e. remove) package content. Furthermore, package manager will notice when two packages offer file with same name, i.e. detect conflicts.

If you place files manually and then install some package that contains file with same name, your manually copied file will be silently overwritten, i.e. programs depending on it will break. While the risk seems small, why take it at all?
Topic archived. No new replies allowed.