避免 typedef c++ 中的冲突声明错误

发布于 2024-12-27 02:07:23 字数 888 浏览 6 评论 0原文

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

背景:

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

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

问题:< /strong>

gridlab 中的 mysql.h 和 list.h typedef 一个名为 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'

感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

生生漫 2025-01-03 02:07:23

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

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

当然,这依赖于 gridlab,而不是 #include 来自 MySQL 的任何内容。

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.

最佳解决方案:

1) 保留当前的主程序

   EXAMPLE: "main.cpp"

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

   EXAMPLE: dbaccess.cpp, dbaccess.h

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

您不需要对 gridlab 进行任何引用您的数据库访问代码;您不需要在 dbaccess.* 代码之外引用 mySql 或 mySQL 列表。

问题解决了:)?

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

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...

月亮坠入山谷 2025-01-03 02:07:23

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

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

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

枕梦 2025-01-03 02:07:23

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

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

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;

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文