Author Topic: Create a static library  (Read 7873 times)

Offline BlubBlub

  • Single posting newcomer
  • *
  • Posts: 6
Create a static library
« on: February 09, 2010, 11:15:29 pm »
hey guys (:

I use the wizard for creating a static library.
In the main.c file there is a little sample which I compiled:

Code
int SampleAddInt(int i1, int i2)
{
    return i1 + i2;
}

// A function doing nothing ;)
void SampleFunction1()
{
    // insert code here
}

// A function always returning zero
int SampleFunction2()
{
    // insert code here
   
    return 0;
}


I tried it and it worked ( I got a .a file ) But when I want to use it in a project like:

Code
#include <iostream>

using namespace std;

int main()
{
cout << SampleAddInt ( 12, 14 ) << endl ;
}

I get an error:
Quote
'SimpleAddInt' was not declared in this scope
I linked the .a file, but it didnĀ“t work. :S

Can somebody help me ?

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Create a static library
« Reply #1 on: February 09, 2010, 11:20:25 pm »
Yes, read/learn what a header file is/does.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Create a static library
« Reply #2 on: February 09, 2010, 11:24:45 pm »
Yes, read/learn what a header file is/does.

And how to mix C and C++ sources/libs.

Offline BlubBlub

  • Single posting newcomer
  • *
  • Posts: 6
Re: Create a static library
« Reply #3 on: February 09, 2010, 11:35:11 pm »
Yes, read/learn what a header file is/does.

I know what header files do.

And how to mix C and C++ sources/libs.

Do you mean extern "C" ?
Can you be more specific ?  :(

I have a guess:

Code
#ifndef MAIN_H
#define MAIN_H

extern "C"
int SampleAddInt(int, int ) ;

#endif

It works, but is it that what you meant ?

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Create a static library
« Reply #4 on: February 09, 2010, 11:51:52 pm »
Quote
#ifdef __cplusplus
extern "C" {
#endif
at the top of the header and

Quote
#ifdef __cplusplus
}
#endif
at the bottom.

So you can use the same header with C and C++-sources.

Offline BlubBlub

  • Single posting newcomer
  • *
  • Posts: 6
Re: Create a static library
« Reply #5 on: February 09, 2010, 11:57:19 pm »
thanks jens for your help  :)