__cdecl只是对调用约定的测试。
这是一个 cmake 项目,只有 1 个源文件:
#include <stdio.h>
#define CALL_CONVENTION __cdecl
void CALL_CONVENTION f(int a, int b)
{
printf("%d, %d", a, b);
}
int main()
{
f(1, 2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我用来set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /FA")输出汇编代码。
当我使用 构建时cmake -G "Visual Studio 15",它构建了一个 32 位应用程序,并且一切都在预期中:
...
; Line 12
push ebp
mov ebp, esp
; Line 13
push 2 ; <------- argument 2
push 1 ; <------- argument 1
call _f ; <------- call function
add esp, 8 …Run Code Online (Sandbox Code Playgroud) 我想编写一个接收&str参数并返回的函数Option<&str>.我写了这个:
fn f(text: &str) -> Option<&str> {
if // some condition {
return None;
}
let mut res = String::new();
// write something into res.
// return res
Some(&res[..])
}
Run Code Online (Sandbox Code Playgroud)
但是我收到一个错误:
res活得不够久.
解决这个问题的最佳解决方案是什么?