我正在将Windows中的C++代码转换为在Linux下工作,我遇到了这个函数"vsprintf_s".它是Microsoft特定的功能,所以我想问一下Linux中是否有替代品?谢谢
由于快速的Google搜索未找到任何内容,因此我将尝试在此处询问(由于涉及gcc / clang的许多人都在这里闲逛)__STDC_LIB_EXT1__-gcc / clang 的状态是什么?我们正在开发一个跨平台应用程序,我想使用其中的一些安全范围检查功能<stdio.h>(奇迹般的功能在Visual Studio 2017中可用),但是无法使用Xcode 9.2编译代码。我认为也许Xcode使用的Clang版本已经过时,但是Ubuntu上的gcc 6.3.0的行为相同。我正在尝试将tmpnam_s与以下示例一起使用:
#if defined(__STDC_LIB_EXT1__)
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#else
#error "__STDC_LIB_EXT1__ not defined"
#endif
int main(int argc, char** argv)
{
char t[L_tmpnam_s];
tmpnam_s(t, L_tmpnam_s);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是编译失败,未定义宏:
gcc -std=c11 test.c
test.c:5:2: error: #error "__STDC_LIB_EXT1__ not defined"
#error "__STDC_LIB_EXT1__ not defined"
^~~~~
Run Code Online (Sandbox Code Playgroud)
我是在做错什么,还是对此功能集的支持很差?
Microsoft安全增强功能(例如strncpy_s或)有哪些替代方法_itoa_s?尽管在MS环境中进行开发,但目标是编写可以轻松移植到其他平台的代码。
length += strnlen_s(str[i],sizeof(str[i]));
//create array to hold all strings combined
char joke[length + strnlen_s(preamble, sizeof(preamble)) + 1];
if(strncpy_s(joke, sizeof(joke), preamble, sizeof(preamble)))
{
printf("Error copying preamble to joke.\n");
return 1;
}
//Concatenate strings in joke
for(unsigned int i = 0; i < strCount; ++i)
{
if(strncat_s(joke, sizeof(joke), str[i], sizeof(str[i])))
{
Run Code Online (Sandbox Code Playgroud)
joiningstring.c:32:3: warning: implicit declaration of function ‘strnlen_s’ [-Wimplicit-function-declaration]
joiningstring.c:38:2: warning: implicit declaration of function ‘strncpy_s’ [-Wimplicit-function-declaration]
joiningstring.c:48:3: warning: implicit declaration of function ‘strncat_s’ [-Wimplicit-function-declaration]
/tmp/ccBnGxvX.o: In function `main':
joiningstring.c:(.text+0x163): undefined reference …Run Code Online (Sandbox Code Playgroud) 最近有一个关于SO的问题(为什么有人会使用strncpy而不是strcpy?),这是hade的答案(答案1,答案2),这使我不确定其他字符串函数的名字中带有'n',如snprintf(我一直在广泛使用).snprintf可以安全使用吗?一般来说,'n'家族的安全功能是什么?
我可以对单词指针数组进行排序,使它们按字母顺序排序,问题是我需要对整数数组(特定单词的使用次数)进行排序,以便整数与它们的位置相同各自的话:
我的代码:
for (i = 0; i < numWords; i++) {
// prints out the words and their frequency respectively
printf("%s - %d\n", dictionary[i], frequency[i]);
}
//sorts the dictionary so that the words are 'alphabetical'
qsort(dictionary, numWords, sizeof(char *), rstrcmp);
printf("\nafter qsort\n"); //checkmark
for (i = 0; i < numWords; i++) {
// prints the word list alphabetically, but the frequencies are no longer matched
printf("%s - %d\n", dictionary[i], frequency[i]);
}
Run Code Online (Sandbox Code Playgroud)
...比较功能V.
int rstrcmp(const void *p1, const void *p2) { …Run Code Online (Sandbox Code Playgroud) 在回答这个问题时,我在Ideone上编译了代码并得到了这个错误
implicit declaration of function ‘scanf_s’ [-Wimplicit-function-declaration]
Run Code Online (Sandbox Code Playgroud)
是不是stdio.h标题scanf_s?
我正在尝试使用 C11 函数strtok_s,该函数应该在 中定义<string.h>,但是当我尝试使用它时,clang 给了我一个链接错误。编译这个程序:
#include <string.h>
int main() {
char * buffer;
char * state;
rsize_t strmax = 0;
char * fpl = strtok_s(buffer, &strmax, "\n", &state);
}
Run Code Online (Sandbox Code Playgroud)
给我这个错误:
repro.c:7:15: warning: implicit declaration of function 'strtok_s' is invalid in
C99 [-Wimplicit-function-declaration]
char * fpl = strtok_s(buffer, &strmax, "\n", &state);
^
repro.c:7:9: warning: incompatible integer to pointer conversion initializing
'char *' with an expression of type 'int' [-Wint-conversion]
char * fpl = strtok_s(buffer, &strmax, "\n", &state); …Run Code Online (Sandbox Code Playgroud) 我正在为Mac OS X上移植一个为Windows编写的应用程序.
在此应用程序中,有许多_vscwprintf和_vscprintf实例.
这个问题帮助我在Mac OS X上实现_vsprintf.但是_vswprintf的相同技术无效.
任何人都可以在Mac OS X上提供_vscwprintf的替代方案吗?或者有任何等效的方法吗?
我正在尝试在Visual Studio 2010中编译OpenHMD.我开始使用Win32库项目布局.我注意到stdafx.cpp已创建并删除了该文件以及生成的标头.现在我仍然得到像这样的编译错误(内联注释):
OHMD_APIENTRY int ohmd_ctx_probe(ohmd_context* ctx)
{
memset(&ctx->list, 0, sizeof(ohmd_device_list));
int i; // <-- error C2143: syntax error : missing ';' before 'type'
for(i = 0; i < ctx->num_drivers; i++){ // <-- error C2065: 'i' : undeclared identifier
ctx->drivers[i]->get_device_list(ctx->drivers[i], &ctx->list);
}
return ctx->list.num_devices;
}
Run Code Online (Sandbox Code Playgroud)
我在哪里可以强制进行纯C编译或设置C语言级别C99?这似乎是C89问题?
注意:我已经改变了for循环
for(int i = 0; ...
Run Code Online (Sandbox Code Playgroud)
至
int i;
for( i = 0; ...
Run Code Online (Sandbox Code Playgroud)