Developer forums (C::B DEVELOPMENT STRICTLY!) > Development

how to create and use shared library in codeblocks

(1/1)

Rahul:
hi to all
 i've centos 6.6 and codeblocks 16.01. i want to build shared object library file. I chosed as

new->project->shared library,  then language c then project title as  "int_valid". now i gave following code in ( under project int_valid )

intvalidation.c
----------------

--- Code: --- #include "intvalidation.h"

int intvalidation(const char * iint)
{
    int i = 0, count = 0, len;
    //unsigned long long int num = 0;

    char * strnum = 0, chnum = 0;

    strnum = (char *) malloc(sizeof (20));

    //sprintf(strnum, "%d", iint);

    len = strlen(iint);

    printf(" len = %d ", len);

    for( count = 0; count < len; count++)
    {
        chnum = (*(iint + count )) ;

        i = chnum - 48;
        if( i >=  0 && i <= 9 )
            continue;
        else
        {
           // printf("\n number is not valid \n");
            return 0;
        }
    }

    return 1;
}

--- End code ---
in intvalidation.h
-------------------

--- Code: ---#ifndef INTVALIDATION_H
#define INTVALIDATION_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int intvalidation(const char * iint);

#endif

--- End code ---
in man.c file of second project :-
----------------------------------

--- Code: ---#include <stdio.h>
#include <stdlib.h>

int intvalidation(char *iint);
int AddInt(int i1, int i2);

int main()
{
    unsigned int x, y, sum = 0;
    char *num1, *num2;

    num1 = (char *)malloc(6);

    printf("\n Enter number x = ");
    scanf("%s", num1);

    if(!intvalidation(num1))
    {
        printf("\n number %s is invalid \n", num1);
        exit(1);
    }

    x = atoi(num1);

    num2 = (char*) malloc(6);

    printf( "\n Enter number to add: ");
    scanf("%s", num2);

    if(!intvalidation(num2))
    {
        printf("\n number %s is invalid \n", num2);
        exit(1);
    }

    y = atoi(num2);

    //sum = x + y;
    sum = AddInt(x, y);

    printf("\n %d + %d = %d", x, y, sum);

    return 0;
}

--- End code ---

in this project i want ot check validity of intger and making a shared library file for this

validation

now when i compile first project ( int_valid project having file file intvalidatoin.c and

intvalidatoin.h)
this generates sharedfile as :-

"liblibint_vaild.so"

why it is having two "lib"  not a single one lib.
and second thing that how to link library file in our project.

BlueHazzard:

--- Quote ---now when i compile first project ( int_valid project having file file intvalidatoin.c and

intvalidatoin.h)
this generates sharedfile as :-

"liblibint_vaild.so"
--- End quote ---

Look into Project->Properties->Build targets->Output filename
If there is written "libint_vaild" remove the first "lib", or uncheck the option "Auto-generate filename prefix" i don't know why this is done...


--- Quote ---and second thing that how to link library file in our project.
--- End quote ---
1) use the include file of the library so you don't have to forward declare the functions.
by this use

--- Code: ---#include "intvalidation.h"
--- End code ---
in min.c and add the include path to the project by
Project->Build options->select the project name on the left->Search directories->Compiler->add: add the path to the "intvalidation.h" file

2) now you have to add the library and the library path:
Project->Build options->select the project name on the left->Search directories->Linker->add: add the path to the "libint_vaild.so" file
for the library:
Project->Build options->select the project name on the left->Linker settings->Add: add "libint_vaild" Ok

now all should work... (maybe you have to add the ".so" to the library, but i am no sure)


Rahul:

hi thanks to reply. i done as you said but having this problem:-
i am unable to add header file (intvalidation.h)
i did like this :  Project->Build options->serch directories->Linker->)path to library(/opt/projects/codeblocks/c/int_valid/bin/Release)
also i given "int_valid" file name in "linker settings" in buid-options. but unable to include header file (intvalidation.h).
and also project build options->search dir -> compiler "/opt/projects/codeblocks/c/int_valid"

how to do that please suggest me.

BlueHazzard:
Please post a full rebuild log:
http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_(general)#Q:_How_do_I_report_a_compilation_problem_on_the_forums.3F

Navigation

[0] Message Index

Go to full version