strcpy 在 Windows 上编译,但在 Linux 上不编译
strcpy compiles on Windows but it doesn't on Linux
我正在学习C++并尝试编写通用代码(抱歉,我不知道您如何调用可以在Windows,Linux,MacOS等上编译的代码(。
我已经编写了函数trimLeft:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const string rows = "rows:";
const string columns = "cols:";
const string data = "data:";
struct dimensions {
int rows;
int columns;
};
inline bool exists (const string& name) {
ifstream f(name.c_str());
return f.good();
}
string trimLeft(const string& input) {
if ((input.empty()) ||
((input.at(0) != ' ') && (input.at(0) != 't')))
return input;
else {
char * tab2 = new char[input.length() + 1];
char *trimmed = new char[input.length() + 1];
strcpy(tab2, input.c_str());
bool skip = true;
int pos = 0;
for (unsigned int i = 0; i < (input.length() + 1); i++) {
if (skip) {
if ((tab2[i] == ' ') || (tab2[i] == 't'))
continue;
else {
skip = false;
trimmed[pos] = tab2[i];
pos++;
}
}
else {
trimmed[pos] = tab2[i];
if (tab2[i] == ' ')
break;
else
pos++;
}
}
string stringTrimmed(trimmed);
return stringTrimmed;
}
}
它在Windows上编译,显示以下警告:
警告 C4996:"strcpy":此函数或变量可能不安全。 请考虑改用strcpy_s。要禁用弃用,请使用 _CRT_SECURE_NO_WARNINGS。
但在 Linux 中,使用以下命令:
g++ FormatMatrix.cpp -o 格式
我得到:
error: ‘strcpy’ was not declared in this scope
每个操作系统上的标头是否不同?
注:
请停止投反对票:我收到了信息。
作为特定编译器的实现细节,<cstring>
,包括strcpy
声明,完全有可能被你包含的另一个标头包含(也许在包含树的更远的地方(。
为了确保你的代码是真正可移植的,并且符合标准,请包括你调用的每个类和函数的头文件;永远不要想当然地认为通过包含不同的东西来获得另一个头文件的功能。
相关文章:
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 如何在linux终端中同时编译和运行c++代码
- 在Linux for Windows上编译C++代码时出错
- 如何在 Mac 上正确编译C++,当它在 Linux 上编译没有问题时?
- 编译在我的 Mac 上工作,但在集群 (Linux) 上不起作用
- 在Linux上使用Clang / OLLVM交叉编译helloworld Windows可执行文件时的问题
- 在 Linux 中使用 -fvisibility=hidden 编译时出现库加载错误
- Steam 回调在 Linux / GCC 编译期间触发警告
- SFML 交叉编译 for Windows on Linux.
- 编译问题与 Ubuntu Linux
- 在Linux上交叉编译Windows应用程序时如何链接到Winsock?
- 在 Linux 上编译 - 在函数 '_start' 中:(.text+0x20):对 'main' 的未定义引用
- 在Linux/Ubuntu中编译openCV应用程序时,如何解决未定义的引用cv:..?
- Stringstream编译错误仅发生在linux环境中
- C++ 当我使用 iostream 时无法编译(Linux,CodeBlocks)
- 在Windows?上编译Linux OpenGL应用程序
- 在OS X上编译Linux应用程序 - 未找到标准库(CMATH)
- 交叉编译 linux-->windows:使用 opengl32 获得更新的 OpenGL 支持.dll
- 将未定义的引用交叉编译linux到windows
- 在没有运行库的情况下编译Linux下的c++