Author Topic: how to create and use shared library in codeblocks  (Read 213 times)

Offline Rahul

  • Single posting newcomer
  • *
  • Posts: 5
how to create and use shared library in codeblocks
« on: March 31, 2018, 01:41:54 pm »
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: [Select]
#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;
}
in intvalidation.h
-------------------
Code: [Select]
#ifndef INTVALIDATION_H
#define INTVALIDATION_H

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

#endif
in man.c file of second project :-
----------------------------------
Code: [Select]
#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;
}

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.

« Last Edit: March 31, 2018, 01:45:15 pm by Rahul »

Online BlueHazzard

  • Lives here!
  • ****
  • Posts: 1780
Re: how to create and use shared library in codeblocks
« Reply #1 on: March 31, 2018, 03:18:58 pm »
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"

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.
1) use the include file of the library so you don't have to forward declare the functions.
by this use
Code: [Select]
#include "intvalidation.h"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)



Offline Rahul

  • Single posting newcomer
  • *
  • Posts: 5
Re: how to create and use shared library in codeblocks
« Reply #2 on: April 01, 2018, 12:43:14 pm »

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.
« Last Edit: April 01, 2018, 12:47:15 pm by Rahul »