错误:为“构造函数”指定的参数数量错误;属性

发布于 2024-11-19 00:27:26 字数 1060 浏览 3 评论 0原文

在实际实现之前,我写了一小段原型代码,并将类构造函数和ctor构造函数放在同一个文件中,看看ctor是否会先执行,这就是我的实际实现。

但是,我面临一个错误。这是代码:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <iostream>

using namespace std;

extern "C" void startMe(void) __attribute__ ((constructor(1)));
extern "C" void ending(void) __attribute__ ((destructor));

class Test {

 public:

    Test()
    {
      cout << "This is test constructor" << endl;
    }
 };

 int main()
 {
  Test();
  printf("Now main called\n");
 }

 void startMe(void)
 {
  printf("Start me called before main\n");
 }

 void ending(void)
 {
  printf("Destructor called\n");
 }

--

Output:
 $ g++ constructor1.cc 
 constructor1.cc:10: error: wrong number of arguments specified for ‘constructor’ attribute

但是,当我删除构造函数优先级时,它可以正常编译并运行。也就是说,我想:

extern "C" void startMe(void) __attribute__ ((constructor)); 

为什么会这样?如何优先?

请帮我。我的想法是“ctor”应该首先执行,然后是另一个(测试)构造函数。同样的原因,我将 ctor 列为优先级 1。

Before the actual implementation, i wrote a small prototype code, and put a class constructor and ctor constructor in the same file, to see if the ctor would execute first, which is my actual implementation.

However, i am facing an error. Here is the code:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <iostream>

using namespace std;

extern "C" void startMe(void) __attribute__ ((constructor(1)));
extern "C" void ending(void) __attribute__ ((destructor));

class Test {

 public:

    Test()
    {
      cout << "This is test constructor" << endl;
    }
 };

 int main()
 {
  Test();
  printf("Now main called\n");
 }

 void startMe(void)
 {
  printf("Start me called before main\n");
 }

 void ending(void)
 {
  printf("Destructor called\n");
 }

--

Output:
 $ g++ constructor1.cc 
 constructor1.cc:10: error: wrong number of arguments specified for ‘constructor’ attribute

However, when i remove the constructor priority, it compiles and runs fine. That is, i do:

extern "C" void startMe(void) __attribute__ ((constructor)); 

Why is it so? How to give priority?

Please help me. My idea is "ctor" should be executed first, then the other (Test) constructor. The same reason, i have put ctor as a priority 1.

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

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

发布评论

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

评论(2

简单 2024-11-26 00:27:26

按原样编译程序会产生:

warning: constructor priorities from 0 to 100 are reserved for the implementation

将优先级从 1 更改为 101 会消除警告,并且可执行文件会产生:

 Start me called before main
 This is test constructor
 Now main called
 Destructor called

This is using GCC 4.5

Compiling your program as is yields:

warning: constructor priorities from 0 to 100 are reserved for the implementation

Changing the priority from 1 to 101 gets rid of the warning and the executable produces:

 Start me called before main
 This is test constructor
 Now main called
 Destructor called

This is using GCC 4.5

夏见 2024-11-26 00:27:26

为“构造函数”属性指定的参数数量错误

看起来您正在使用 GCC 的下层版本。

根据 GCC 4.2.1 文档,以下是相关的 GCC 4.2.1 函数属性

构造函数
析构函数
    构造函数
属性导致该函数之前被自动调用
执行进入main()...

以及相关的GCC 4.3.0 函数属性

构造函数
析构函数
构造函数(优先)
析构函数(优先级)
    构造函数
属性导致该函数之前被自动调用
执行进入main()...

解决办法是使用GCC 4.3以上版本。

我目前正在 OpenBSD 5.7 上测试一些软件,它附带 GCC 4.2.1 编译器。我们还支持 CentOS 5,它附带 GCC 4.1 编译器。代码如下所示:

// INIT_PRIORITY manages initialization of C++ static objects. Under GCC, 
// the library uses init_priority attribute in the range [INIT_PRIORITY,
// INIT_PRIORITY+100]. Under Windows, INIT_PRIORITY enlists
// "#pragma init_seg(lib)". Undefine or set to 0 to disable it.
#define INIT_PRIORITY 250

#ifdef __GNUC__
# define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#endif
#ifdef __clang__
# define CLANG_VERSION (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
#endif
...

#if __GNUC__ && INIT_PRIORITY && ((GCC_VERSION >= 40300) || (CLANG_VERSION >= 20900))
DLL void API DetectX86Features() __attribute__ ((constructor (INIT_PRIORITY + 50)));
DLL bool API CpuId(word32 input, word32 *output);
#elif __GNUC__ && INIT_PRIORITY
DLL void API DetectX86Features() __attribute__ ((constructor));
DLL bool API CpuId(word32 input, word32 *output);
#else
DLL void API DetectX86Features();
DLL bool API CpuId(word32 input, word32 *output);
#endif

您可能应该创建一个附加类,例如 Initialization,并将 startMe 放入构造函数中,将 ending 放入析构函数中。然后,创建 C++ 对象的静态实例,如初始化 init;

避免静态初始化顺序惨败,你应该使用 init_priority(另请参阅此 Stack Overflow 问题属性的说明GCC 邮件列表上的 init_priority)。 init_priority以来一直存在至少 GCC 3.2

wrong number of arguments specified for ‘constructor’ attribute

It looks like you are using a downlevel version of GCC.

According to the GCC 4.2.1 docs, the following are the relevant GCC 4.2.1 Function Attributes:

constructor
destructor
    The constructor
attribute causes the function to be called automatically before
execution enters main ()...

And the relevant GCC 4.3.0 Function Attributes:

constructor
destructor
constructor (priority)
destructor (priority)
    The constructor
attribute causes the function to be called automatically before
execution enters main ()...

The solution is to use GCC 4.3 or above.

I'm currently testing some software on OpenBSD 5.7, and it ships with the GCC 4.2.1 compiler. We also support CentOS 5, and that ships with the GCC 4.1 compiler. Here's what out code looks like:

// INIT_PRIORITY manages initialization of C++ static objects. Under GCC, 
// the library uses init_priority attribute in the range [INIT_PRIORITY,
// INIT_PRIORITY+100]. Under Windows, INIT_PRIORITY enlists
// "#pragma init_seg(lib)". Undefine or set to 0 to disable it.
#define INIT_PRIORITY 250

#ifdef __GNUC__
# define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#endif
#ifdef __clang__
# define CLANG_VERSION (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
#endif
...

#if __GNUC__ && INIT_PRIORITY && ((GCC_VERSION >= 40300) || (CLANG_VERSION >= 20900))
DLL void API DetectX86Features() __attribute__ ((constructor (INIT_PRIORITY + 50)));
DLL bool API CpuId(word32 input, word32 *output);
#elif __GNUC__ && INIT_PRIORITY
DLL void API DetectX86Features() __attribute__ ((constructor));
DLL bool API CpuId(word32 input, word32 *output);
#else
DLL void API DetectX86Features();
DLL bool API CpuId(word32 input, word32 *output);
#endif

You should probably create an additional class, like Initialization, and put startMe in the constructor and ending in the destructor. Then, create a static instance of the C++ object, like Initialization init;.

To avoid the static initialization order fiasco, you should use init_priority (also see this Stack Overflow question and Clarification of attribute init_priority on the GCC mailing list). init_priority has been around since at least GCC 3.2.

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