错误:为“构造函数”指定的参数数量错误;属性
在实际实现之前,我写了一小段原型代码,并将类构造函数和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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
按原样编译程序会产生:
将优先级从 1 更改为 101 会消除警告,并且可执行文件会产生:
This is using GCC 4.5
Compiling your program as is yields:
Changing the priority from 1 to 101 gets rid of the warning and the executable produces:
This is using GCC 4.5
看起来您正在使用 GCC 的下层版本。
根据 GCC 4.2.1 文档,以下是相关的 GCC 4.2.1 函数属性:
以及相关的GCC 4.3.0 函数属性:
解决办法是使用GCC 4.3以上版本。
我目前正在 OpenBSD 5.7 上测试一些软件,它附带 GCC 4.2.1 编译器。我们还支持 CentOS 5,它附带 GCC 4.1 编译器。代码如下所示:
您可能应该创建一个附加类,例如
Initialization
,并将startMe
放入构造函数中,将ending
放入析构函数中。然后,创建 C++ 对象的静态实例,如初始化 init;
。避免静态初始化顺序惨败,你应该使用
init_priority
(另请参阅此 Stack Overflow 问题和属性的说明GCC 邮件列表上的 init_priority)。init_priority
自 以来一直存在至少 GCC 3.2。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:
And the relevant GCC 4.3.0 Function Attributes:
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:
You should probably create an additional class, like
Initialization
, and putstartMe
in the constructor andending
in the destructor. Then, create a static instance of the C++ object, likeInitialization 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.