问题描述
我有一个奇怪的.我正在使用供应商的头文件开发嵌入式系统.我正在使用 GCC 4.6.3 编译文件.我想在我的代码中使用 C++,我有我无法弄清楚的错误.我正在运行一个供应商示例程序,我所做的只是将 main.c 文件的名称更改为 main.cpp.因此,我假设头文件正在由 C++ 编译器解释.其中之一包含以下几行:
I have a weird one. I'm working on an embedded system, using the vendors header files. I'm compiling the files using GCC 4.6.3. I want to use C++ for my code, I have error I can't figure out. I'm running a vendor example program, and all I've done is changed the name of the main.c file to main.cpp. As a result, I assume, the header files are being interpreted by the C++ compiler. One of them contains the following lines:
__attribute__((naked)) static return_type signature \
{ \
__asm( \
"svc %0\n" \
"bx r14" : : "I" (number) : "r0" \
); \
}
如果文件名是 main.c,则文件编译正确,我认为这是因为该文件正在由 C 编译器处理.如果我使用 C++,我得到的错误是
The files compile properly if the name of the file is main.c, I assume this is because the file is being processed by the C compiler. The error I get if I use C++ is
error: impossible constraint in 'asm'
但是,我对 C 编译器没有任何问题.我需要调用在 C++ 文件中使用此定义的函数.我曾考虑编写留在 c 端并链接到它们的包装函数,但这将是一个真正的痛苦,而且效率较低.有什么建议?
But again, I have no problem with the C compiler. I need to call functions that use this define in C++ files. I've considered writing wrapper functions that stay on the c side and linking to them, but it would be a real pain, and less efficient. Any suggestions?
推荐答案
svc
也称为swi
是ARM/Thumb 软件中断指令.它只需要常量,但它们与其他寄存器常量不同.即,mov r0, #4096
.如果您希望指定立即数,则需要使用预处理器和令牌粘贴.number
不能是变量或寄存器.
svc
also known as swi
is the ARM/Thumb software interrupt instruction. It only takes constants, but they are different from other register constants. Ie, mov r0, #4096
. You need to use the preprocessor and token pasting if you wish to specify an immediate. number
can not be a variable or register.
#define syscall(number) __attribute__((naked)) static return_type signature \
{ \
__asm( \
"svc " #number "\n" \
"bx r14" : : : "r0" \
); \
}
会起作用.注意: #
是 'C' 预处理器的字符串化.另请注意,查看 SVC
编号是低效的,因为它在 I-CACHE
中并且检查需要 D-CACHE
.通常它总是 constant
并且 function 号被传递到一个寄存器中以加快系统调用的速度.
will work. Note: The #
is the 'C' preprocessors stringify. Also note that it is in-efficient to look at the SVC
number as it is in the I-CACHE
and inspecting requires D-CACHE
. Generally it is always constant
and the function number is passed in a register for faster syscall's.
gcc 手册说,
'I'- 作为数据中的立即数操作数有效的整数处理指令.也就是说,范围为 0 的整数到 255 旋转 2 的倍数
这是典型的数据处理操作数 - 立即数,ARM ARM 的 A5.1.3 节.SVC
操作数在拇指模式下是固定的 8 位或在 ARM 模式下是固定的 24 位.可能还有一些我不知道的其他约束,但只要将数字常量传递给宏,至少预处理器的字符串化将起作用.
This is typical of Data-processing operands - immediate, section A5.1.3 of the ARM ARM. The SVC
operands are either fixed 8-bits in thumb mode or fixed 24-bits in ARM mode. It maybe possible with some other constraint that I am unaware of, but at least the preprocessor's stringification will work as long as a numeric constant is passed to the macro.
我想幸运的是这在 gcc
中有效,不幸的是 g++
没有.您可以通过使用 -S
并使用这两种工具查看(和发布)输出来进一步了解.
I guess it is lucky that this worked from gcc
and unlucky that g++
did not. You can get further insight by using -S
and looking at (and posting) the output using both tools.
您的代码似乎适用于 gcc-4.7.2
,但 number
是一个 const int
在我的情况下是本地的,使用 number
可能是问题所在.也许它有一个微妙的语义变化,从C"到C++".
Your code does seem to work with gcc-4.7.2
, but number
is a const int
local in my case, the use of number
maybe the issue. Perhaps it has a subtle semantic change from 'C' to 'C++'.
这篇关于在 GNU 的 C++ 代码中使用 C 头文件.包含内联汇编的错误:'asm' 中的不可能约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!