4

I am trying to write a program that should use a C library (the LIS library) in a C++ program. There seems to be a problem with the creation/initialization of struct objects.

When I run the example program on the wikipediapage: http://en.wikipedia.org/wiki/Lis_%28linear_algebra_library%29 it runs like a charm, but of course that is compiled as a C program.

In my C++ code I do it as follows:

#include "stdafx.h"
#include <iostream>

extern "C"
{
#include "lis.h"
#include "lis_config.h"
    LIS_MATRIX A;

}
using namespace std;


int main(LIS_INT argc, char* argv[])
{
    lis_initialize(&argc, &argv);
    lis_matrix_create(LIS_COMM_WORLD, &A);
    getchar();

    return 0;
}

When I run this code, it gives me an access violation at the line lis_matrix_create. It seems as though A has an memory address, its data members (LIS_MATRIX is defined as a struct in Lis.h) have not been initialized, and therefore their addresses are NULL.

Could you please tell me how to create the LIS_MATRIX in such a way that I can use it like it is done in the example code on the wikipedia page?

Thank you in advance!

In reply to Adam and Ross Ridge: I use visual studio 2013 on Windows 7 64 bit. The manual of the Lis library states that it is compatible with the Visual Studio 2008, 2010 and 2012 compilers, and also with gcc 3.4 and 4.4 and some IBM, Intel and PGI C++ compilers, I hope Visual Studio 2013 will not be a problem.

Also, in this code, if I take out the 'extern C' block, and include 'stdio.h' instead of iostream, it runs without problems (so I guess that it means the C compiler is used?). The minute I also include iostream, the access violation start.

DrDonut
  • 864
  • 14
  • 26
  • 5
    why are you putting the declaration `LIS_MATRIX A` inside the `extern "C"` block? – pqnet Aug 13 '14 at 21:58
  • 2
    Why do you have `LIS_MATRIX A;` inside the `extern "C" {}` block? – R Sahu Aug 13 '14 at 21:58
  • 1
    Nothing in this code strikes me as odd. If you two see anything suspicious in having the variable be an `extern` global, I'm not seeing it. – Mooing Duck Aug 13 '14 at 22:40
  • 2
    Why use `extern "C"` at all? That keyword gives `A` C linkage (i.e. won't mangle its name), but nothing is linked against this code so that has no benefit. – Adam Aug 14 '14 at 00:08
  • 1
    DrDonut, have you tried simply compiling the wiki example code with the C++ compiler? You can call C routines from C++, and you rarely have to make any accommodations to do so. – Adam Aug 14 '14 at 00:12
  • What C compiler did you compile the library with and with what C++ compiler did you compile the your example code? – Ross Ridge Aug 14 '14 at 04:50
  • Thank you all for your responses! @pqnet, R Sahu, Mooning Duck, Adam: Yes I have tried to put LIS_Matrix A in the main function instead of in the extern block, it did not make a difference. – DrDonut Aug 14 '14 at 06:22
  • 1
    and it should make no difference, as long as LIS_MATRIX type is declared and define in header insisde extern linkage – CoffeDeveloper Aug 14 '14 at 07:02

1 Answers1

1

You are including

lis_config.h

after

lis.h

wich is per se an error(you have to include it before). Also if you touched anything in lis_config you have to rebuild the whole library (using most same compilers flag of your project, for example "-msee2" if you used SSE2). Before rebuilding just swap headers only to see if that is enough..

A few more words: a library can easily detect headers included in wrong order, make a ticket to lis developers for that.

CoffeDeveloper
  • 7,961
  • 3
  • 35
  • 69
  • 1
    Thank you so much! Although the order of lis.h and lis_config.h does not seem to be the problem, you helped me see that the order of the header files might be an influence. It turns out that if I include after lis.h it works, but if I include it before lis.h it breaks. Do you think I should submit a ticket for this? – DrDonut Aug 14 '14 at 07:32
  • 1
    absolutely yes, there could be a flaw in library design if there are conflicts with standard headers (even if those are C++ from a C library). – CoffeDeveloper Aug 14 '14 at 08:56