4

有没有办法让 g++ 忽略或解决冲突的 typedef?

背景:

我正在为 gridlab_d 模拟器编写一些 C++ 代码。我的模型需要连接到 c++ 数据库,所以我使用的是 mysql++ 库。使用 mysql++ 库需要我链接到 mysql 库,所以我编译

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

问题:

gridlab typedef 中的 mysql.h 和 list.h 都是一个名为 LIST 的结构。这是编译器错误

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'

谢谢你的帮助!

4

4 回答 4

6

也许预处理器包含解决您问题的方法。

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

当然,这依赖于 gridlab 没有#include来自 MySQL 的任何东西。

于 2012-01-14T22:16:49.430 回答
2

最佳解决方案:

1) 保留你当前的主程序

   EXAMPLE: "main.cpp"

2)为您的数据库访问编写一个新模块

   EXAMPLE: dbaccess.cpp, dbaccess.h

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

您的 dbaccess 代码中不需要任何对 gridlab 的引用;您不需要在 dbaccess.* 代码之外引用 mySql 或 mySQL 列表。

问题解决了 :)?

PS:如果你真的需要某种可以在不同模块之间共享的“列表”,我鼓励你使用标准 C++“vector<>”之类的东西。恕我直言...

于 2012-01-14T22:24:35.643 回答
0

我假设您在多个文件中使用 SSQLS。您是否阅读过有关在多个文件中使用 SSQLS 的说明。

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

于 2012-01-14T22:12:47.163 回答
0

有两种可能性——两种列表类型要么兼容,要么不兼容。如果它们兼容,您可以将定义复制到一个新的标题中,并从每个位置包含该定义。如果它们不兼容,您将不得不更改其中一个名称。

编辑:这是我通过谷歌搜索找到的两个结构定义:

MySQL:

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

网格实验室:

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;

看着那些,似乎您不会将它们按摩成同一类型。更改其中一个名称 - 通过进行大量搜索/替换或使用一些巧妙的#define技巧 - 请注意,如果您选择后一条路线,您不会犯任何错误。

于 2012-01-14T22:13:45.170 回答