Jump to content

Your/my own library

- - - - -

  • Please log in to reply
11 replies to this topic

#1
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
So, I've decided to make my own library. If I can even call it that. It's nothing special, contains few general functions, for now :)

Basicly, I'd like something like this:

#include <myLib.h>

So, I have *.h and *.cpp files and I somehow managed to make *.o files but I guess I'm not linking them right. I'm using Code::Blocks and set paths under Settings > Compiler and debugger > Search directories > Includes tab for header files and Linker tab for *.o files (I'm not sure if this is right)

When I try to compile I get 'undefined reference' error. Also, I've read that in header files I should put extern word infront of function prototype. I've also got a class and as I understand you don't/can't put extern infront of class? And is use in this case correct?

template <class Type>

extern const Type someFunction ( const Type& a, const Type& b );


A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#2
Groogy

Groogy

    Programmer

  • Members
  • PipPipPipPip
  • 183 posts
Well if it's Code::Blocks there should already be a "Dynamic" or "Static" library template. You should start out with that one if it's your first time. And the resulting file you get should be .so or .a depending if you took Dynamic or Static. And that about extern is when it's a dynamic library I think. Anyway you have to define them like this when compiling the actual library if it's C++:


extern "C" {

  int myFunc();

}


Or else that function will get name-mangled.

*EDIT*
I don't know how it works with template functions. But if I remember right template-functions must be written inline in a header file to be used elsewhere, you can't have a prototype. Am I wrong?

I can also recommend this page: Linux Tutorial - Static, Shared Dynamic and Loadable Linux Libraries
But it uses no IDE but you should get the general idea of how it works.
My Code Blog - My Github - Ascension Project - Madness Script Project - Simple-Garbage-Collector Project
There is bound to be something useful somewhere.

#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Still stuck on this and running out of ideas. Accually, I've none left. I've put extern "C" in my header files, and even the code and still getting error: if I try to compile *.hpp file I get linker input file unused because linking not done, and if I try to compile *.cpp file with or without source code in it I get error: template with C linkage.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#4
xiao

xiao

    Newbie

  • Members
  • PipPip
  • 13 posts
Don't put templates inside the extern "C" because templates are only C++, not C. Like Groogy said, you should make template functions inline. Put the prototype and implementation into the header file.

read this Dr Dobbs article for separation of implementation and declaration http://www.drdobbs.c...TMY32JVN?pgno=1

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Beaten too it. And you mean "myLib.h" not <myLib.h>?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I do mean <myLib.h>. I think I've set everything in my IDE, just need the librarys.

EDIT: I removed extern "C" code, and now all files compile. But template functions and classes still do not work. I tried to explicitly instantiate them but compiler whines yet again.

Edited by Flying Dutchman, 02 August 2010 - 12:42 PM.

A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
xiao

xiao

    Newbie

  • Members
  • PipPip
  • 13 posts
Still get undefined reference error? Here's an example to be clear:

Method 1: inline function

Header File
#ifndef TEST2_HPP_

#define TEST2_HPP_

#include <iostream>


template <class Type>

void test(Type t);


template <class Type>

inline void test(Type t)

{

	std::cout << t << "\n";

}


#endif

Source File
#include "test2.hpp"


int main()

{

	test(3);

	test("Hello");


	return 0;

}

Method 2:

Header File
#ifndef TEST_HPP_

#define TEST_HPP_


template <class Type>

void test(Type t);


#endif

Source File
#include "test1.hpp"

#include <iostream>

using namespace std;


template <class Type>

void test(Type t)

{

	cout << t << "\n";

}


template void test<char * >(char * t);

template void test<int>(int);


int main()

{

	test(3);

	test("Hello");


	return 0;

}


#8
Groogy

Groogy

    Programmer

  • Members
  • PipPipPipPip
  • 183 posts
Erhm I'm not sure but I don't think method 2 works? I don't usually make my own template functions and classes. But if I'm right then you have to do the entire function inline. At least you have to do that on functions in a normal class that has template functions.
My Code Blog - My Github - Ascension Project - Madness Script Project - Simple-Garbage-Collector Project
There is bound to be something useful somewhere.

#9
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I tried method 2 and it didn't work, but even if it did I wouldn't like it; it wouldn't work if I wanted to use my own classes. Will try method 1 and report how it goes.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#10
xiao

xiao

    Newbie

  • Members
  • PipPip
  • 13 posts
Sorry. It compiled on my compiler (g++ 4.1.2) but maybe I made mistakes. That might be const char instead of char. Here is a link for explanation
C++ primer plus - Google Books

#11
Groogy

Groogy

    Programmer

  • Members
  • PipPipPipPip
  • 183 posts
Look, I believe why you have to have it inline to use it in other/several CPP files is because templates must have their code remade/copied each time you write a new type to the template. And if we have the template-function compiled the compiler can't remake it and replace the code that allocate enough space and find functions for the variables etc. etc. Though I am just spewing what I think and it might not be true.
My Code Blog - My Github - Ascension Project - Madness Script Project - Simple-Garbage-Collector Project
There is bound to be something useful somewhere.

#12
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Finally figured it out! :D I've put template function code into *.hpp files and then created a dummy *.cpp file where I just included *.hpp file. Thanks you all guys for help.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users