Board index » cppbuilder » how to use a c plain language static library?

how to use a c plain language static library?


2003-12-10 09:57:50 AM
cppbuilder53
i have created a static library with c plain language, the content is as follows:
/*****File:test.h***********************/
int add(int a, int b);
int sum(void);
/*****File:test.c***********************/
#include <stdio.h>
#include <stdlib.h>
#include "test.h"
int add(int a, int b){
return a + b;
}
int sum(){
return add(4, 4);
}
then, i use it in a new c++ builder 6.0 project. the content is as follows.
//---------------------------------------------------------------
#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;
//---------------------------------------------------------------
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "test.h"
//---------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner
: TForm(Owner)
{
}
//--------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ShowMessage(IntToStr(add(2, 2)));
ShowMessage(IntToStr(sum()));
}
//--------------------------------------------
then, i compile the project, the error come on:
[Linker Error] Unresolved external 'add(int, int)' referenced from E:\TEMP\UNIT1.OBJ
[Linker Error] Unresolved external 'sum()' referenced from E:\TEMP\UNIT1.OBJ
i really do not know how to do with the question? Please help me?
Thanks anyway.
 
 

Re:how to use a c plain language static library?

You have to add the library to the project.
The file test.h that you show is set up for only the C language. Try
changing it to this:
------------------------
#ifndef TEST_H
#define TEST_H
#ifdef __cplusplus
extern "C" {
#endif
int add(int a, int b);
int sum(void);
#ifdef __cplusplus
}
#endif
#endif
------------------------
The macro __cplusplus is defined when compiling as C++. The construct
extern "C" is specified by the C++ language. You can see it in use in the
header files such as stdio.h
. Ed
Quote
nideshuai wrote in message
news:3fd68bae$ XXXX@XXXXX.COM ...