Trying to make a library

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.
Last edited on
1) The header guards are fine.

2) You don't need main() in mylib.cpp for a static library. Most likely your project is not set up properly for a static library.

3) DLLs are windows shared libraries (*nix uses .so) You would need extra code to make this a shared library.

4) I usually #include where I need it which is usually in .cpp files.
Thanks for the reply.

2) You don't need main() in mylib.cpp for a static library. Most likely your project is not set up properly for a static library.


I'm using Code::Blocks 10.05. Instead of starting a new project and then selecting static library, I just created a new .h file and a new .cpp file. Shouldn't that work just as well?
Nope. The project files pass directives needed for the compiler/linker that are specially suited to what type of project you choose. If you make a WIN32 app it expects WinMain, if you make a console app it expects main() and so on.
I started a new project and selected Dynamic Link Library. Then I wrote mylib.h and mylib.cpp the same as I did above, except that the header guards now say _MYLIB_H_ instead of MYLIB_H_INCLUDED and the int main(){} is no longer in mylib.cpp. Then I wrote test.cpp as a separate file and saved it in the same folder as the project. I also tried including it in the project itself but it still doesn't work. Same error as above, undefined reference to 'dtoa(double)'.
Last edited on
Topic archived. No new replies allowed.