Using and making custom libraries.

Ok, I'd consider myself on an intermediate level with the STL, and I want to move on to creating and using other libraries, but I don't understand how. My questions are.

1) Linking, not sure, but I see in the tutorials I read(which didn't help at all) this pops up a bit. How do you "link" libraries?

2) Creating your own library. I followed a tutorial on this and ended up with me being confused as to what I just did. How do you create a library and what is allowed in a library? For instance, can I create a library with classes, then in another program use this library and instead of coding the classes, just be able to use their data members and functions since it uses the library with the classes already made?

3) Also on a side note, since I am currently making a simple text based RPG, can anyone give me a few libraries that will be good for this type of project? Maybe something that lays out a text-style interface that's easy to use for my level?
1) That depends on what ide you are using.

2) As does this.. (you should somewhere be able to choose to make a .lib when creating a new project)
You can create libs with classes, and use them a if they were a part of your code. You can do this, because libs come with headers that contain the declarations of classes and functions in them.

3) I don't know what lib could help you with making an rpg, but for console stuff try ncurses
No no no no no no no...

1) You link libraries using a linker. GNU Binutils has a good one called ld.
http://www.gnu.org/software/binutils/

To use it, compile your code to object files, and then use ld.

If you're using MSVC++, then there is a pramga directive for linking that simplifies matters....
http://msdn.microsoft.com/en-us/library/7f0aews7(v=VS.71).aspx

It eliminates your need to use ld and your need to compile your code to an object file first.

2) You can create a library using the GNU project's libtool.
http://en.wikipedia.org/wiki/GNU_Libtool

You can indeed include classes in libraries, though it would be a good idea to have a header file, else it could get messy.

3) Irrlicht is a very easy one to use, albeit limited feature-wise.
http://irrlicht.sourceforge.net/

-Albatross

EDIT: September the 11th posts, and counting.
Last edited on
man gcc
Well I tried making one using MSVC, it would let me build the library but when I put it in my program it didn't notice any of the classes declared in the library. How exactly do I use the header? Can anyone give me an example of what it is supposed to look like using just a simple class? I can't seem to find anything online laying out my specific questions.
Here is the code, it's probably not correct at all, but that's why I am posting it. Being self taught sucks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ifndef _RPGLIB_H_
#define _RPGLIB_H_



int random(int min, int max);

struct Entity
{
       string m_name;
       int m_health;
       int m_healthConst;
       
       Entity(){}
       Entity(const char * name, int health)
{
       m_name=name;
       m_health=m_healthConst=health;
       }

};


struct Player;

struct Mob: public Entity
{           
             int m_DmgMax;
             int m_DmgMin;
             int m_knockdownactive;
             
             Mob(const char * name, int health, int dmg_min, int dmg_max, int kd)
{
             m_name=name;
             m_health=health;
             m_healthConst=health;
             m_DmgMin=dmg_min;
             m_DmgMax=dmg_max;
             m_knockdownactive=kd;
             }
             
             void Attack(Player & target);
};
              
struct Player: public Entity
{
       
        int m_def;
        int m_block;
        int m_crit;
        int m_playerDmgMin;
        int m_playerDmgMax;
        int m_stateKD;
        
        int m_level;
        int m_xp;
        
    Player(const char * name, int health, int def, int block, int crit, int kd, int dmg_min, int dmg_max)
    {
        m_name=name;
        m_health=health;
        m_healthConst=health;
        m_playerDmgMin=dmg_min;
        m_playerDmgMax=dmg_max;
        m_def=def;
        m_block=block;
        m_crit=crit;
        m_stateKD=kd;
        m_level=1;
        m_xp=0;
    }

    void Attack(Mob & target);
    void LevelUp();
};

void Battle(Player & player, Mob & mob);



#endif 


So I guess you can't have strings in a library or something? Says C++ does not support default ints. After that, it says each m_name is undeclared. Also, can I make another library to define these class functions? I want to try and leave out classes and function definitions out of the main cpp to reduce confusion. Anyone?
To use strings you have to #include <string> (and don't forget to beusing namespace std;). I think that this is also why compiler ignores the declaration of m_name.

can I make another library to define these class functions?
Um.. What do you mean? You should define those functions in THIS library. That is the only reason for making one..
To reduce confusion you could simply split your code into several .cpp files.
Last edited on
Ok, I think I got it working now. Thing was I did have #include <string> at one point but I forgot using namespace std; I also placed the function definitions in it as well. Now, take a look at the main cpp.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <ctime>


using namespace std;
#include "rpglib.h"
#ifndef _RPGLIB_H_
#define _RPGLIB_H_

int main()
{
  
     srand(time(0));

    Player player("Adeon",100,10,10,5,10,6,16);

    Mob pirate("Pirate",40,5,10,2);
    Mob bandit("Bandit",50,10,15,2);
    Mob wildbear("Wild Bear",70,10,20,2);
    

Mob * mob_table[3]={&pirate,&bandit,&wildbear};

    bool fight=true;
    int mob_id;
    char c;

    while (fight)
    {
        mob_id=random(0,2);

        Battle(player,*mob_table[mob_id]);

        system("cls");

        cout << "Fight again? (y/n) ->";
        cin>>c;

        if (c=='n' || c=='N') break;
    }
	
	
    system("cls");
    cout << "Thank you for playing!" << endl;
    system("pause");
	return 0;
}


I do include the header right? I know that when I don't, it has no idea what the functions are that I call. But I get this "fatal error C1004: unexpected end-of-file found" when I compile. Do I use #endif ? Because I've tried and it doesn't work, unless I am putting it in the wrong place.
Why did you write lines 10 and 11? They are only needed in the header.
If for some reason you did need to do this, you'd have to add #endif somewhere.
When you put #endif in the end of file, since _RPGLIB_H_ is already defined in your header compiler will ignore everything between #ifndef and #endif .
Mainly because I have no idea what I am doing. I'm trying to learn how to use libraries other than the standard ones. But this doesn't really answer my question. The problem I am having is even when I #include "rpglib.h" it does not recognize the functions called in the main cpp. So I really have no idea what to do at this point. The header file builds without problem, but the main cpp acts as if it doesn't exist at all.
Nevermind, I figured it out. I switched from using bloodshed to MSVC, so I had to figure out the compiler more than anything. Problem was I fixed the header file, but forgot to put then new header file into the project folder for the main cpp, now it works perfectly. Now I can continue learning other libraries. Thanks for all the help.
Topic archived. No new replies allowed.