我正在尝试使用 SWIG 包装带有变量参数的 C 函数,如下所示。
void post(const char *fmt, ...)
{
char buf[MAXPDSTRING];
va_list ap;
t_int arg[8];
int i;
va_start(ap, fmt);
vsnprintf(buf, MAXPDSTRING-1, fmt, ap);
va_end(ap);
strcat(buf, "\n");
dopost(buf);
}
Run Code Online (Sandbox Code Playgroud)
但是当我在 Lua 中运行该函数时,它仅在我使用 1 个参数时才有效。我无法用下面的风格来写。
pd.post("NUM : %d", 123);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误。
Error in post expected 1..1 args, got 2
Run Code Online (Sandbox Code Playgroud)
是否可以使用 SWIG 用变量参数包装 C 函数?
我将不胜感激任何帮助。谢谢!
我正在尝试将double转换为float指针,但我不知道正确的方法.
这是我的代码
#include <iostream>
int main(int argc, const char * argv[]) {
// insert code here...
float *f;
double d = 10;
f = reinterpret_cast<float*>(&d);
d = 20;
std::cout << "RESULT : " << *f << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到以下结果.
RESULT : 0
Program ended with exit code: 0
Run Code Online (Sandbox Code Playgroud)
如何正确地将double转换为浮点指针?
ADD:我期望得到的结果是20
在hello.cpp文件中,我有这个静态函数。
static void hello()
{
std::cout << "hello" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我想从world.h文件中的其他静态函数调用这个函数,如下所示。
static void world()
{
hello();
std::cout << "world" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,最推荐的公开hello()其他文件的方法是什么?
我想知道如何从Lua C API中的函数获取多个返回值。
Lua代码:
function test(a, b)
return a, b -- I would like to get these values in C++
end
Run Code Online (Sandbox Code Playgroud)
C ++代码:(调用函数的部分)
/* push functions and arguments */
lua_getglobal(L, "test"); /* function to be called */
lua_pushnumber(L, 3); /* push 1st argument */
lua_pushnumber(L, 4); /* push 2nd argument */
/* call the function in Lua (2 arguments, 2 return) */
if (lua_pcall(L, 2, 2, 0) != 0)
{
printf(L, "error: %s\n", lua_tostring(L, -1));
return;
}
int ret1 = …Run Code Online (Sandbox Code Playgroud) 我正在尝试找到一种使用 pybind11 构建和运行带有嵌入式 python 解释器的 cpp 文件的方法。
在本教程中,它使用 CMake,但我正在寻找一种无需 CMake 即可完成此操作的方法。
这是我尝试过的。
在示例.cpp中:
#include <pybind11/embed.h> // everything needed for embedding
namespace py = pybind11;
int main() {
py::scoped_interpreter guard{}; // start the interpreter and keep it alive
py::print("Hello, World!"); // use the Python API
}
Run Code Online (Sandbox Code Playgroud)
当我运行以下命令时,在终端中:(构建良好)
c++ -O3 -Wall -std=c++11 -undefined dynamic_lookup `python3 -m pybind11 --includes` example.cpp -o example
Run Code Online (Sandbox Code Playgroud)
然后运行二进制文件
./example
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
dyld:未找到符号:_PyBaseObject_Type 引用自:/Users/cuinjune/Desktop/pybindtest/./example 预期位置:/Users/cuinjune/Desktop/pybindtest/./example 中的平面命名空间 zsh:中止 ./example
有没有可能的方法可以使用 pybind11 正确构建和执行带有嵌入式 python 解释器的 cpp 文件?(不使用 CMake)
我正在寻找一种\n在Lua中表达新行()的替代方法,因为我的宿主应用程序不允许用户使用\字符。
这是我的代码:
local str = "Hello\nWorld"
print(str)
Run Code Online (Sandbox Code Playgroud)
有没有其他解决方案可以在不使用\n字符的情况下制作相同的字符串?
我是Lua的新手,我刚刚发现以下代码在Lua中是合法的.
local abc = 123
local abc = 345
print(abc)
Run Code Online (Sandbox Code Playgroud)
以上和以下之间有什么区别吗?
local abc = 123
abc = 345
print(abc)
Run Code Online (Sandbox Code Playgroud) 我在 Lua 表中有函数t,我了解到我可以t通过运行以下脚本列出包含的所有函数:
for k, v in pairs(t) do
print(k, v)
end
Run Code Online (Sandbox Code Playgroud)
例如,它将打印:
myfunction1 function: 0x107219805
myfunction2 function: 0x10721c194
myfunction3 function: 0x1071e067c
Run Code Online (Sandbox Code Playgroud)
现在,我想知道是否有可能获得每个函数期望的参数数量。所以结果可能如下所示:
myfunction1 function: 0x107219805 arguments: 3
myfunction2 function: 0x10721c194 arguments: 4
myfunction3 function: 0x1071e067c arguments: 5
Run Code Online (Sandbox Code Playgroud)
这在 Lua 中可能吗?还是使用 C API?
我想在Lua中重命名一些变量以便于使用。
我可以简单地使用不同的名称来创建别名,但是我担心会浪费一些内存。
在这种情况下,我可以简单地分配nil给旧变量吗?
这是我的示例代码:(尝试重命名my.tab为myTab)
print(type(my.tab))
myTab = my.tab
my.tab = nil
print(type(my.tab))
print(type(myTab))
Run Code Online (Sandbox Code Playgroud)
结果:
table
nil
table
Run Code Online (Sandbox Code Playgroud)
尽管这种方法似乎可行,但我想问一下这是否是一种安全且正确的方法来重命名Lua中的变量。