Hi. I'm trying to create my first library. I have the following header file I created, called mylib.h:
1 2 3 4 5 6 7 8
|
#include<sstream>
#ifndef MYLIB_H_INCLUDED
#define MYLIB_H_INCLUDED
std::string dtoa(double);
#endif // MYLIB_H_INCLUDED
|
And the corresponding mylib.cpp file:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include "mylib.h"
std::string dtoa(double d)
{
std::stringstream ss;
std::string s;
ss<<d;
ss>>s;
ss.clear();
return(s);
}
int main() {}
|
Now I have a test file where I try to use the function from the library:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include "mylib.h"
#include<cstdlib>
#include<iostream>
int main()
{
std::string s;
double d=45.6;
s=dtoa(d);
std::cout<<s<<std::endl;
system("pause");
return 0;
}
|
I compiled mylib.cpp. But I am getting the following error when trying to compile the test file:
E:\Sud-Chemie\test2.o:test2.cpp|| undefined reference to `dtoa(double)'|
||=== Build finished: 1 errors, 0 warnings ===|
Any idea what I'm doing wrong? Also, some questions:
1.) What should the format of the header guards be? I've noticed a few different formats but I don't understand the difference. I thought they're just there to prevent the header file from being included more than once.
2.) In mylib.cpp, I included
int main(){}
at the bottom to get rid of the "undefined reference to WinMain@16" error. Is this the right way to do it?
3.) Will this create a dynamic link library, static library, or shared library. I believe the difference between a dynamic link library and a static library is that the dynamic link library is not built into the program and is called during runtime. Is that right? And what exactly is a shared library? From what I've heard, it sounds exactly like a dynamic link library, assuming my definition of that is even correct.
4.) Where should headers like #include<sstream> go? In the .h file, .cpp file, or both?
Thank you.