LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   New project: creating a C++ library to be used from C: Linking question (https://www.linuxquestions.org/questions/programming-9/new-project-creating-a-c-library-to-be-used-from-c-linking-question-885635/)

ejspeiro 06-10-2011 01:44 PM

New project: creating a C++ library to be used from C: Linking question
 
Hi there!

I'm working on a summer project which seeks to build a C++ library for the resolution of Partial Differential Equations (PDE's).

The first project which shall use that library is a Reactive Transport Modelling (RTM) software which is written in C.

I was reading about using classes and general C++ code from a C code from [1], but I noticed that I need to ask about the general compiler issues of writing a library, so here yields my question.

In [1] you can read the following example of compiling:

Quote:

Suppose you have C program files main.o, f1.o, and f2.o, and you use a C++ library helper.a. With Sun C++, you would issue the command

Code:

CC -o myprog main.o f1.o f2.o helper.a

What does the ".a" file extension means exactly? I have installed libraries which have a lot of those but so far, I have only used them but how can I create them?

Thanks in advanced for your help guys!

Sergei Steshenko 06-10-2011 01:52 PM

Quote:

Originally Posted by ejspeiro (Post 4382132)
...
What does the ".a" file extension means exactly? I have installed libraries which have a lot of those but so far, I have only used them but how can I create them?

Thanks in advanced for your help guys!

I think 'a' means archive. You create them using linker ('ld') and maybe 'ar' utility.

Start from

man ld
man ar

and look up on the web tutorials, etc.

The main problem though is that C++ and "C" use different naming conventions on object files.

ejspeiro 06-10-2011 02:02 PM

Ummm... yes! You need an extra application for that!

I just read it here: http://www.network-theory.co.uk/docs...tro/index.html

It seems like a nice book! I was actually thinking in getting it, have you guys seen it before?

Sergei Steshenko 06-10-2011 02:12 PM

Quote:

Originally Posted by ejspeiro (Post 4382144)
Ummm... yes! You need an extra application for that!

I just read it here: http://www.network-theory.co.uk/docs...tro/index.html

It seems like a nice book! I was actually thinking in getting it, have you guys seen it before?

You have to realize that 'ld' and 'ar' are separate from 'gcc' programs - even though 'gcc' implicitly calls 'ld' when needed. I.e. you have to understand in detail how 'ld' works because it will be 'ld' which reports unresolved symbols during linking should such a problem occur.

...

The book may still be nice though.

ejspeiro 06-10-2011 02:15 PM

Thank you Sergei!

I am aware that they are actually separate applications ;)

I shall take a deeper look to "ld".

Regards!

John VV 06-10-2011 02:44 PM

you might want to have a look at Gmic or the original greystoration in CImg.h
it uses a pde ( heat flow) to edit photos

ejspeiro 06-10-2011 02:46 PM

John VV:

Thanks! I will look at that!

Actually, the library will implement Mimetic Finite Difference Methods! Which are quite amazing!

ejspeiro 06-10-2011 03:16 PM

0k guys here's the first attempt!

I created the following class in m.h:

PHP Code:

class {
  public:
    
int m_f1(int);
  private:
    
int i;
    
int j;
}; 

with following related implementation file m_f1.cc:

PHP Code:

#include "m.h"

int m::m_f1(int i) {

  return 
i;


Nice... now, according to [1], I should create a wrapper function per each member function of the class I want to use in the C code. Therefore, I created a "wrapper implementation file" per each implementation file... lets call that file C_m_f1.c:

PHP Code:

#include "wm.h"

#ifdef __cplusplus
  
extern "C"
#endif
int C_m_f1(int ii) {

  
*m1 = new m;

  return 
m1->m_f1(ii);


Notice that all the "wrapper implementation files" are declared in wm.h:

PHP Code:

#include "m.h"

#ifdef __cplusplus
  
extern "C"
#endif
int C_m_f1(int ii); 

Finally, the main.c file goes like this:

PHP Code:

#include <stdio.h>

#include "wm.h"

int main () {

  
printf("We will try to access the function from the class m...\n");
  
printf("Check: %d it worked!\n"C_m_f1(2));
  return 
0;


How am I compiling? I'm doing this:

Code:

$ g++ -Wall -c m_f1.cc
$ g++ -Wall -c C_m_f1.cc
$ gcc -Wall m_f1.o C_m_f1.o main.c -o main
In file included from wm.h:1,
                from main.c:3:
m.h:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘m’

But I get the error mentioned above :(

Sergei Steshenko 06-10-2011 03:33 PM

Quote:

Originally Posted by ejspeiro (Post 4382207)
0k guys here's the first attempt!

I created the following class in m.h:

PHP Code:

class {
  public:
    
int m_f1(int);
  private:
    
int i;
    
int j;
}; 

with following related implementation file m_f1.cc:

PHP Code:

#include "m.h"

int m::m_f1(int i) {

  return 
i;


Nice... now, according to [1], I should create a wrapper function per each member function of the class I want to use in the C code. Therefore, I created a "wrapper implementation file" per each implementation file... lets call that file C_m_f1.c:

PHP Code:

#include "wm.h"

#ifdef __cplusplus
  
extern "C"
#endif
int C_m_f1(int ii) {

  
*m1 = new m;

  return 
m1->m_f1(ii);


Notice that all the "wrapper implementation files" are declared in wm.h:

PHP Code:

#include "m.h"

#ifdef __cplusplus
  
extern "C"
#endif
int C_m_f1(int ii); 

Finally, the main.c file goes like this:

PHP Code:

#include <stdio.h>

#include "wm.h"

int main () {

  
printf("We will try to access the function from the class m...\n");
  
printf("Check: %d it worked!\n"C_m_f1(2));
  return 
0;


How am I compiling? I'm doing this:

Code:

$ g++ -Wall -c m_f1.cc
$ g++ -Wall -c C_m_f1.cc
$ gcc -Wall m_f1.o C_m_f1.o main.c -o main
In file included from wm.h:1,
                from main.c:3:
m.h:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘m’

But I get the error mentioned above :(

Your 'main.c' includes a C++ (not "C") file, but you compile your 'main.c' using 'gcc' (not 'g++'). I.e. ultimately you are feeding "C" compiler ('gcc') with a C++ file ('m.h'), and 'gcc' does not like it.

...

You problem has nothing to do with the thread name which is about linking and not compilation.

ejspeiro 06-10-2011 03:50 PM

Well, I believe the problem is because of the linking within the compilation effort.

I'm trying to follow: http://developers.sun.com/solaris/ar...tml#cpp_from_c

But it is a pretty vague tutorial.

Sergei Steshenko 06-10-2011 04:03 PM

Quote:

Originally Posted by ejspeiro (Post 4382244)
Well, I believe the problem is because of the linking within the compilation effort.

I'm trying to follow: http://developers.sun.com/solaris/ar...tml#cpp_from_c

But it is a pretty vague tutorial.

Your 'main.c' does not have to know anything about C++ code you have - this is common sense - regardless of tutorial.

ejspeiro 06-10-2011 04:09 PM

Yes I had thought about that :) I still have to read better to see how can I feed the wrapper function from the C++ information but without having my main file knowing about that.

Sergei Steshenko 06-10-2011 04:13 PM

Quote:

Originally Posted by ejspeiro (Post 4382255)
Yes I had thought about that :) I still have to read better to see how can I feed the wrapper function from the C++ information but without having my main file knowing about that.

Create a 'c_wrappers.h' file (you can choose a different name, of course) which is to contain just prototypes of your "C" wrapper functions and it should not (and need not) contain any C++ code and should not include files with C++ code.

Use this file both when implementing your "C" wrapper functions and in your 'main.c'.

ejspeiro 06-10-2011 04:24 PM

That is why I created my wm.h file, but because of what you say, it does not work... But know I wonder how do the wrappers within that proposed file (c_wrappers.h) would know about the class details?

Sergei Steshenko 06-10-2011 04:36 PM

Quote:

Originally Posted by ejspeiro (Post 4382269)
That is why I created my wm.h file, but because of what you say, it does not work... But know I wonder how do the wrappers within that proposed file (c_wrappers.h) would know about the class details?

A function prototype is just its "header" with args and their type, i.e for

Code:

double add2(double a, double b)
  {
  return a + b;
  }

the prototype is

Code:

double add2(double, double);
/*
or you can write it as

double add2(double a, double b);

- any arg names are OK, they are discarded anyway
*/

, so nothing in the prototype needs to be known about the implementation.


All times are GMT -5. The time now is 07:59 AM.