ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I only know use the command g++ filename.cpp -o filename to compile a exetcuable file so far, recently, I want compile a library, the code is from <<thinking in c++>>
Code:
//: C04:CLib.h
// Header file for a C-like library
// An array-like entity created at runtime
typedef struct CStashTag {
int size; // Size of each space
int quantity; // Number of storage spaces
int next; // Next empty space
// Dynamically allocated array of bytes:
unsigned char* storage;
} CStash;
void initialize(CStash* s, int size);
void cleanup(CStash* s);
int add(CStash* s, const void* element);
void* fetch(CStash* s, int index);
int count(CStash* s);
void inflate(CStash* s, int increase);
///:~
another file:
Code:
//: C04:CLib.cpp {O}
// Implementation of example C-like library
// Declare structure and functions:
#include "CLib.h"
#include <iostream>
#include <cassert>
using namespace std;
// Quantity of elements to add
// when increasing storage:
const int increment = 100;
void initialize(CStash* s, int sz) {
s->size = sz;
s->quantity = 0;
s->storage = 0;
s->next = 0;
}
int add(CStash* s, const void* element) {
if(s->next >= s->quantity) //Enough space left?
inflate(s, increment);
// Copy element into storage,
// starting at next empty space:
int startBytes = s->next * s->size;
unsigned char* e = (unsigned char*)element;
for(int i = 0; i < s->size; i++)
s->storage[startBytes + i] = e[i];
s->next++;
return(s->next - 1); // Index number
}
void* fetch(CStash* s, int index) {
// Check index boundaries:
assert(0 <= index);
if(index >= s->next)
return 0; // To indicate the end
// Produce pointer to desired element:
return &(s->storage[index * s->size]);
}
int count(CStash* s) {
return s->next; // Elements in CStash
}
void inflate(CStash* s, int increase) {
assert(increase > 0);
int newQuantity = s->quantity + increase;
int newBytes = newQuantity * s->size;
int oldBytes = s->quantity * s->size;
unsigned char* b = new unsigned char[newBytes];
for(int i = 0; i < oldBytes; i++)
b[i] = s->storage[i]; // Copy old to new
delete [](s->storage); // Old storage
s->storage = b; // Point to new memory
s->quantity = newQuantity;
}
void cleanup(CStash* s) {
if(s->storage != 0) {
cout << "freeing storage" << endl;
delete []s->storage;
}
} ///
What command I should use?
Thank you.
Last edited by Xiangbuilder; 10-19-2003 at 03:13 AM..
We know a c++ program must have an int main function, but there is no in main function in the file. So I guess the files is used to create a library file such as *.lib, Only is my guess, in fact, I don't if g++ can be used to create a linrary or not.
Here is the error information:
Code:
[root@localhost a_a]# g++ CLib.cpp -o CLib
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crt1.o(.text+0x18): In function `_start':
../sysdeps/i386/elf/start.S:77: undefined reference to `main'
collect2: ld returned 1 exit status
[root@localhost a_a]#
Thank you.
I find some sentences below in the book: "To test the library, two CStashes are created. The first holds ints and the second holds arrays of 80 chars:"
Code:
//: C04:CLibTest.cpp
//{L} CLib
// Test the C-like library
#include "CLib.h"
#include <fstream>
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
int main() {
// Define variables at the beginning
// of the block, as in C:
CStash intStash, stringStash;
int i;
char* cp;
ifstream in;
string line;
const int bufsize = 80;
// Now remember to initialize the variables:
initialize(&intStash, sizeof(int));
for(i = 0; i < 100; i++)
add(&intStash, &i);
for(i = 0; i < count(&intStash); i++)
cout << "fetch(&intStash, " << i << ") = "
<< *(int*)fetch(&intStash, i)
<< endl;
// Holds 80-character strings:
initialize(&stringStash, sizeof(char)*bufsize);
in.open("CLibTest.cpp");
assert(in);
while(getline(in, line))
add(&stringStash, line.c_str());
i = 0;
while((cp = (char*)fetch(&stringStash,i++))!=0)
cout << "fetch(&stringStash, " << i << ") = "
<< cp << endl;
cleanup(&intStash);
cleanup(&stringStash);
} ///
How to compile the file and test the library, in other words, what command I should use?
So far, I can successfully compiled the source code by the command
Can you list (ls) the contents of your working directory and print it here, please.
There should be a file called libCLib.a in your working (i.e. current / compiling / active ) directory, created by the line,
ar rcs libCLib.a CLib.o
I would stick with this for now, then move on to shared libraries (bottom), purely for the sake of keeping things simple and not having to worry about anything like the dynamic linker setup. I am assuming you are clear on the differences between shared (*.so) and static (*.a) libraries.
N.B. As mr_segfault says, you can specify the location of this library file using the -L compiler option, although it's probably just easier to get started by working in one directory.
"Can you list (ls) the contents of your working directory and print it here, please."[code]
[root@localhost a_a]# ls
CLib.cpp CLib.h CLibTest.cpp
[root@localhost a_a]# g++ -c CLib.cpp
[root@localhost a_a]# ls
CLib.cpp CLib.h CLib.o CLibTest.cpp
[root@localhost a_a]# ar ruv CLib.a CLib.o
a - CLib.o
[root@localhost a_a]# ls
CLib.a CLib.cpp CLib.h CLib.o CLibTest.cpp
[root@localhost a_a]# g++ CLibTest.cpp -o CLibTest -L. -lCLib
/usr/bin/ld: cannot find -lCLib
collect2: ld returned 1 exit status
[root@localhost a_a]#
, it works not very good. It is because of my poor knowledge instead of lenlutz' command, lenlutz just want to show me the method, but I can't use the method correct.
Sorry, I am not clear on the diffrences between shared (*.so) and static (*.a).
Thank you.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.