Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is needed to get my own Package Library working?

Status
Not open for further replies.

Alex314

Programmer
May 22, 2008
6
NL
I cannot find how to access a home made Package Library from an application. I use Codegear C++ builder 2007.
First I coded the Package and it produced a .bpl file. I copied it to a directory that can be found.
Then I made the application that needs the package. Nothing I tried caused the application to compile. The error messages I get say that there is an "...Error: Unresolved external 'A::getValue() const' referenced from D:\RAD PROJECTEN\BPL-TEST\TEST01APPLCATIE\DEBUG_BUILD\UNIT1.OBJ".

I now have the code as follows:
mylib.cpp:
Code:
#pragma hdrstop

#include "mylib.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)
//---------------------------------------------- 
#include "mylib.h"

class A;

A::A(int newa) : a(newa) 
{ 
}

int A::getValue() const 
{ 
  return a; 
}

void A::setValue(int newa) 
{ 
  a = newa; 
}
mylib.h:
Code:
#ifndef mylibH
#define mylibH
//---------------------------------------------------------------------------

class A;

class A 
{ 
   public: 
      A(); 
      ~A() {}; 
      A(int newa); 
      int getValue() const; 
      void setValue(int newa); 
   private: 
      int a; 
};
//----------------------------------------------

#endif
Unit1.cpp:
Code:
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

#include "mylib.h"

TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  // Creates an object, A (from the library)
  A test(50);
  Form1->Caption = test.getValue();
}
Unit1.h:
Code:
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
  TButton *Button1;
  void __fastcall Button1Click(TObject *Sender);
private:	// User declarations
public:		// User declarations
  __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

What is missing?

Regards, Alex
 
I'm not very familiar with packages so when I'm telling you is from my interpretation of C++Builder Developer's Journal's six part article named, Packages in C++Builder. These started in July of 2008 but you have to be a subscriber to read them. The cost is well worth it in my humble opinion.

When I've seen this error, it usually relates to exporting functions or namespaces. In packages, like DLLs, you have to export your functions and classes. Unlike DLLs, you don't have to create a custom "define". You use the keyword PACKAGE.

Packages in C++Builder said:
If you’ve ever created a DLL project then you’ll know you need to use the __declspec( dllimport ) and __declspec( dllexport ) declarations, and the following examples will look familiar.

#ifdef _BUILDDLL_ // DLL project define
#define APITYPE __declspec( dllexport )
#else
#define APITYPE __declspec( dllimport )
#endif
APITYPE int func();
class APITYPE TMyClass
{
int i;
virtual int func( void );
};

The declaration difference between a DLL project and a Package project is that you don’t need to create a custom “define” for the latter type. Instead, you use the PACKAGE keyword, like so:

PACKAGE int func();
class PACKAGE TMyClass
{
int i;
virtual int func( void );
};

This simple keyword simplifies project development a little—and every bit helps.

When creating namespaces in packages, there are some rules to follow.

Packages in C++Builder said:
The name given to the namespace has a very specific naming convention. It must bare the same name as the unit that contains it, but the first character must be a capital letter and all others lowercase.

This advice is from skimming over those six articles and I don't know if they will help or not but these were the first things that came to my mind.


James P. Cottingham
I'm number 1,229!
I'm number 1,229!
 
James,
Thanks for your reply. I'll study it, though I don't know if it is enough to get me going. Maybe I am on the wrong track wanting to use a Library Package and perhaps I should go back to a DLL. I don't think I want to spend $49 for a journal that most probably is over my head!
I will look into DLL's and see where it gets me.
Regards, Alex
 
Also, try looking at the #pragma link directive. This directs the linker to link in the library.
 
Thanks Prattaratt, I will come to that I expect. I have begun to study the examples given in a book by Hollingworth et al and working with CPPB 5. It will increase my understanding of the subject (I hope!)
Regards, Alex
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top