调用Glib::spawn_async_with_pipes报无匹配函数编译错误求助
在C++中使用Glib::spawn_async_with_pipes时的编译错误问题
问题描述
我在C++代码里调用Glib::spawn_async_with_pipes函数时遇到编译错误,虽然通过nm -D /lib/x86_64-linux-gnu/libglibmm-2.4.so | grep "spawn_async_with_pipes"命令确认该函数存在于库中,但编译依然失败。
编译报错信息
main.cpp:44:37: error: no matching function for call to ‘spawn_async_with_pipes(const char [2], <brace-enclosed initializer list>, Glib::SpawnFlags, sigc::slot<void, int, int>, GPid*, Glib::RefPtr<Glib::IOChannel>*, Glib::RefPtr<Glib::IOChannel>*, std::nullptr_t, std::nullptr_t)’ 44 | Glib::spawn_async_with_pipes(".", // Working directory | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~ 45 | {"bash", "-c", command}, // Command to execute | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 46 | Glib::SPAWN_DEFAULT, // Spawn flags | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 47 | sigc::slot<void, Glib::Pid, int>( | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 48 | sigc::mem_fun(*this, &TerminalWindow::on_subprocess_exit)), // Callback | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 49 | &pid, // Process ID | ~~~~~~~~~~~~~~~~~~~~~~ 50 | &stdin_channel, // Standard input channel | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 51 | &stdout_channel, // Standard output channel | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 52 | nullptr, // Standard error channel | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 53 | nullptr // Main context | ~~~~~~~~~~~~~~~~~~~~~~~~ 54 | );
使用的编译命令
g++ main.cpp pkg-config --cflags --libs gtkmm-3.0 glib-2.0 gio-2.0
引发问题的代码片段
void handle_command() { Gtk::TextIter start, end; terminal_buffer->get_bounds(start, end); std::string command = terminal_buffer->get_text(start, end); // ... Create pipes for input and output Glib::RefPtr<Glib::IOChannel> stdin_channel, stdout_channel; int exit_status = 0; // Spawn the subprocess with pipes for input and output Glib::spawn_async_with_pipes(".", // Working directory {"bash", "-c", command}, // Command to execute Glib::SPAWN_DEFAULT, // Spawn flags sigc::slot<void, Glib::Pid, int>( sigc::mem_fun(*this, &TerminalWindow::on_subprocess_exit)), // Callback &pid, // Process ID &stdin_channel, // Standard input channel &stdout_channel, // Standard output channel nullptr, // Standard error channel nullptr // Main context ); }
解决方法
1. 修正命令行参数类型
spawn_async_with_pipes的第二个参数要求是std::vector<std::string>类型,直接使用初始化列表会因为包含std::string对象导致类型不匹配。改成显式声明vector:
std::vector<std::string> argv = {"bash", "-c", command}; Glib::spawn_async_with_pipes(".", argv, Glib::SPAWN_DEFAULT, sigc::slot<void, Glib::Pid, int>( sigc::mem_fun(*this, &TerminalWindow::on_subprocess_exit)), &pid, &stdin_channel, &stdout_channel, nullptr, nullptr );
2. 确保回调函数签名一致
报错显示推导的回调签名是sigc::slot<void, int, int>,和你声明的sigc::slot<void, Glib::Pid, int>不符。检查on_subprocess_exit函数,必须严格匹配以下签名:
void on_subprocess_exit(Glib::Pid pid, int exit_status) { // 你的处理逻辑 }
3. 修复编译命令格式
编译命令里pkg-config部分必须用反引号包裹,否则g会把它当成源文件处理;同时要链接glibmm-2.0(C封装库)而非glib-2.0(C库):
g++ main.cpp `pkg-config --cflags --libs gtkmm-3.0 glibmm-2.0 gio-2.0`
4. 确认变量pid的类型
确保pid的类型是Glib::Pid(对应C库的GPid),避免因类型不匹配导致参数错误。
内容的提问来源于stack exchange,提问作者RAT
相关产品推荐
相关产品推荐

