如何解决gcc [-Werror=format-security] in function call?

How to solve gcc [-Werror=format-security] in function call?

我有这个电话给 czmq api:

int rc = zsock_connect(updates, ("inproc://" + uuidStr).c_str());
(Note: uuidStr is of type std::string and zsock_connect expects a const char* as its second argument)

编译器错误:

error: format not a string literal and no format arguments [-Werror=format-security]
int rc = zsock_connect(updates, ("inproc://" + uuidStr).c_str());
                                                               ^                                                                                                    

我试过:

const char* connectTo = ("inproc://" + uuidStr).c_str();
int rc = zsock_connect(updates, connectTo);

还有

int rc = zsock_connect(updates, (const char*)("inproc://" + 
uuidStr).c_str());

但错误仍然存​​在。

我该如何纠正?

上下文;我正在尝试使用 pip install 将此代码编译为 Linux 上的 Python 扩展。在 Windows 上,它使用 pip install 编译并运行得很好,大概是编译器更宽松。

这个函数就像 printf() 和朋友一样,对吧?如果是这样,你会遇到与 printf(some_var) 存在的相同问题 - 如果你传递的字符串中包含格式序列,你会得到未定义的行为和不好的事情发生,因为没有你提供的参数'告诉功能期望。解决方法是执行以下操作:

int rc = zsock_connnect(updates, "inproc://%s", uuidStr.c_str());

基本上,给它一个格式,将您的字符串作为参数。