Find the answer to your Linux question:
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.
  1. #1
    Just 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?

  2. #2
    Just 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

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,512
    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!

  4. #4
    Just Joined!
    Join Date
    Aug 2010
    Posts
    32
    Quote Originally Posted by Rubberman View Post
    School project? You can't have a main() in a library.
    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

  5. #5
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Here's how to create a shared(shared object) library in Linux..I'm assuming that's what your after

    testit.h
    Code:
    #ifndef SORT_H
    #define SORT_H
    
    void asort(int x[], int y);
    void dsort(int x[], int y);
    
    #endif
    testit.cpp
    Code:
    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;
    			}
    		}
    	}
    }
    test.cpp
    Code:
    #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;
    }
    And finally the compile lines

    g++ -fPIC -c testit.cpp
    ld -shared -o testit.so testit.o
    g++ test.cpp ./testit.so -o test
    Last edited by gerard4143; 11-09-2010 at 10:50 PM.
    Make mine Arch Linux

  6. #6
    Just 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •