Results 1 to 6 of 6
I'm trying to make one that has some sorts in it for now.
I know you have to make a header file and a .cpp file.
I'm using VC++(yes, I'm ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 11-09-2010 #1Just Joined!
- Join Date
- Aug 2010
- Posts
- 32
How do you make a library in C++
I'm trying to make one that has some sorts in it for now.
I know you have to make a header file and a .cpp file.
I'm using VC++(yes, I'm in school so windows in needed. I use Ubuntu at home I swear D
if it makes a difference.
What goes in the header file, how is the cpp file set up, and then where do I put the files once I'm done?
- 11-09-2010 #2Just Joined!
- Join Date
- Aug 2010
- Posts
- 32
Alright well this is my header
#ifndef SORT_H
#define SORT_H
void asort(int x[], int y);
void dsort(int x[], int y);
#endif
Here is the .cpp file my sorts are in
void asort(int x[],int n) {
int y,z,t;
for (y=1;y<=n;y++) {
for (z=0;z>=n-2;z++) {
if (x[z]<x[z+1]) {
t = x[z];
x[z] = x[z+1];
x[z+1] = t;
}
}
}
}
void dsort(int x[],int n) {
int y,z,t;
for (y=1;y<=n;y++) {
for (z=0;z<=n-2;z++) {
if (x[z]<x[z+1]) {
t = x[z];
x[z] = x[z+1];
x[z+1] = t;
}
}
}
}
And here is my main with the includes
#include "stdafx.h"
#include "sort.h"
#include <iostream>
using namespace std;
void main()
{
int x[10],y(10),z;
for (z=0;z<=9;z++) {
cout << "Enter: ";
cin >> x[z];
}
asort(x,y);
for (z=0;z<=9;z++)
cout << x[z];
}
All I get is build errors, and I'm gonna shoot and guess it has to do with me trying to call asort. What am I doing wrong
- 11-09-2010 #3Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 10,160
School project? You can't have a main() in a library.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 11-09-2010 #4Just Joined!
- Join Date
- Aug 2010
- Posts
- 32
The main isn't. The main is what is trying to run a function from the library. The library is the chunk of code above it.
I've tried googling the answers but everything on there is either about C or is just not making any sense.
In BASIC all I had to do was type LIBRARY "path here"
There has to be an easy or similar way to do this in C++, and I'm assuming that is what the #include "fdasgfdag.h" stuff is, however it seems to not be letting me use the functions in the library
- 11-09-2010 #5
Here's how to create a shared(shared object) library in Linux..I'm assuming that's what your after
testit.h
testit.cppCode:#ifndef SORT_H #define SORT_H void asort(int x[], int y); void dsort(int x[], int y); #endif
test.cppCode:void asort(int x[], int n) { int y, z, t; for (y=1; y <= n; y++) { for (z=0;z>=n-2;z++) { if (x[z]<x[z+1]) { t = x[z]; x[z] = x[z+1]; x[z+1] = t; } } } } void dsort(int x[],int n) { int y, z, t; for (y = 1; y <= n; y++) { for (z=0;z<=n-2;z++) { if (x[z]<x[z+1]) { t = x[z]; x[z] = x[z+1]; x[z+1] = t; } } } }
And finally the compile linesCode:#include <iostream> #include "testit.h" using namespace std; int main() { int x[10], y(10), z; for (z = 0; z <= 9; z++) { cout << "Enter: "; cin >> x[z]; } asort(x,y); for (z=0;z<=9;z++) cout << x[z]; return 0; }
g++ -fPIC -c testit.cpp
ld -shared -o testit.so testit.o
g++ test.cpp ./testit.so -o testLast edited by gerard4143; 11-09-2010 at 10:50 PM.
Make mine Arch Linux
- 11-10-2010 #6Just Joined!
- Join Date
- Sep 2007
- Posts
- 19
Are you sure it gives no compiler errors/warnings?
I am expecting an error/warning in the asort call:
asort(x,y) // x and y are arrays (pointers if you will)
asort defn. is asort(int[], int) // a pointer and a normal int
Try fixing that and check. But I am not sure if this question should be on the forum :P


Reply With Quote

