4

Is there a way I can make g++ ignore or work around conflicting typedefs?

Background:

I'm writing some c++ code for the gridlab_d simulator. My model needs to connect to a c++ database, so I'm using the mysql++ library. use of the mysql++ library requires me to link to the mysql library, so i compile with

g++ -I/usr/include/mysql -I/usr/local/include/mysql++

Problem:

both mysql.h and list.h in gridlab typedef a struct to have the name LIST . Here is the compiler error

In file included from /usr/include/mysql/mysql.h:76, 
             from /usr/include/mysql++/common.h:182,
             from /usr/include/mysql++/connection.h:38,
             from /usr/include/mysql++/mysql++.h:56,
             from direct_data.cpp:21:
/usr/include/mysql/my_list.h: At global scope:
/usr/include/mysql/my_list.h:26: error: conflicting declaration 'typedef struct st_list LIST'
../core/list.h:22: error: 'LIST' has a previous declaration as 'typedef struct s_list LIST'

Thanks for your help!

Vikas Yendluri
  • 341
  • 3
  • 7
  • 13

4 Answers4

6

Perhaps the preprocessor contains a solution to your problem.

#define LIST GRIDLAB_LIST
#include <gridlab_include_file.h>
#undef LIST

This relies, of course, on gridlab not #includeing anything from MySQL.

Omnifarious
  • 54,333
  • 19
  • 131
  • 194
  • @VikasYendluri - It basically causes the pre-processor to do a simple textual replacement of `LIST` with `GRIDLAB_LIST` everyplace where `LIST` occurs. Since `LIST` is a typedef, there is no chance that (even with C++) the name is ever mentioned in the resulting object file. It will cause no compiling or linking problems. – Omnifarious Jan 14 '12 at 22:22
  • Unless there is code that needs to use both kinds of list type, this is a good start. Actually - even then it's OK as long as you use GRIDLAB_LIST in the right spots in the shared code. – Carl Norum Jan 14 '12 at 22:44
  • @CarlNorum - That's what I was thinking. – Omnifarious Jan 14 '12 at 22:49
2

Best solution:

1) Keep your current main program

   EXAMPLE: "main.cpp"

2) Write a new module for your database access

   EXAMPLE: dbaccess.cpp, dbaccess.h

3) #include "dbaccess.h" in main.cpp

You don't need any references to gridlab in your dbaccess code; you don't need to refer to mySql or mySQL lists outside of your dbaccess.* code.

Problem solved :)?

PS: If you really need some kind of "list" that you can share between different modules, I'd encourage you to use something like a standard C++ "vector<>". IMHO...

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • This also an idea, and a lot cleaner than my idea. :-) – Omnifarious Jan 14 '12 at 22:30
  • if I were to do this, I would need to include mysql++.h in dbaccess.h. then i would need to include dbaccess.h in main.cpp. since main.cpp would also include the gridlab.h header, i would still face the same conflicting declaration error. How do I get around this? – Vikas Yendluri Jan 15 '12 at 02:52
  • No, don't include mysql++.h in dbaccess.h, only in dbaccess.cpp. The point is to define your own private abstraction wrapped around the raw MySQL access. (Most of your code shouldn't be coupled to a specific database anyway!) – Alan Stokes Jan 15 '12 at 16:49
0

I assume you are using SSQLS in multiple files. Have you read the instruction about using SSQLS in multiple files.

http://tangentsoft.net/mysql++/doc/html/userman/ssqls.html#ssqls-in-header

Adrian Cornish
  • 23,227
  • 13
  • 61
  • 77
  • Thanks for your response. it turns out, i am not using SSQLS in multiple files. the section you link to refers to defining custom SSQLS schemas for mysql++, my error is from the libraries at large with no custom SSQL from my part. – Vikas Yendluri Jan 14 '12 at 22:14
0

There are two possibilities - either the two list types are compatible, or they're not. If they are compatible, you can just copy the definition off into a new header and include that one from each location. If they're not compatible, you're going to have to change one of the names.

EDIT: Here are the two structure definitions that I found by doing some Google searching:

MySQL:

typedef struct st_list {
  struct st_list *prev,*next;
  void *data;
} LIST;

Gridlab:

typedef struct s_listitem {
    void *data;
    struct s_listitem *prev;
    struct s_listitem *next;
} LISTITEM;

typedef struct s_list {
    unsigned int size;
    LISTITEM *first;
    LISTITEM *last;
} LIST;

Looking at those, it seems like you're not going to massage them into the same type. Change one of the names - either by doing a big search/replace or by using some clever #define tricks - watch out that you don't make any mistakes if you choose the latter route.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469