我开始学习MinGW来编译C++程序.我有一个示例C++文件,包括test.cpp(主程序)和srfft.h(我添加了额外的头文件,而不是来自libray).该过程按以下步骤执行:
g ++ test.cpp -o test.exe
TEST.EXE
#include <iostream>
using namespace std;
int main()
{
cout<< "Hello World!\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到了正确的答案,但是当我在C++代码中添加#include时如下:
#include <iostream>
#include <srfft.h>
using namespace std;
int main()
{
cout<< "Hello World!\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
CMD向我展示了"致命错误:srfft.h:没有这样的文件或目录"
如何使用MinGW执行我的代码?问题出在哪儿?
我正在使用G ++编译器.以下代码正在正确编译.
#include<iostream>
#include<cstdlib>
#include<stdio.h>
using namespace std;
int random(int max)
{
return rand() / (RAND_MAX / max + 1);
}
int main()
{
for(int iteration=0; i<10; i++)
{
int myNum=random(130);
myNum=myNum-(myNum%iteration); /* This line causes exception. */
(myNum<0)?(myNum=myNum*-1):myNum;
cout<<"\nRandom number is "<<myNum<<"\n";
}
}
Run Code Online (Sandbox Code Playgroud)
在运行时,它正在生成以下异常..
g++ Implement.cc ./a.out Floating point exception
你能帮我纠正一下这个错误吗?
我试图通过main传递参数,这工作正常,然后检查传入的参数是否包含正确的格式/值.但是,即使我通过正确的格式,它仍然显示有错误,这里是代码:
int main(int argc, char* argv[]) {
/* Check if arguments are being passed through */
if(argc == 1){
cout << endl << "--- ERROR ---" << endl;
exit(0);
}
/* Check if the first argument contains the correct data */
string file_name = argv[1];
/* Handle operation */
string operation = argv[2];
if(operation != "-t" || operation != "-r")
{
cout << "Something is not right";
}
}
Run Code Online (Sandbox Code Playgroud)
如果我这样做:cout << operation;那么结果将是:-t当我运行应用程序时传递-t.
谁能建议我哪里出错了?
更新:
我会传递这些论点:
./main …
我已经为我的班级写了这个程序.我发现它使用GNU g ++编译器编译并运行得很好.我的教授从他的网站自动评分我们的程序,该网站使用Microsoft Visual Studio编译器,它会引发错误.我也在BSD clang编译器中尝试过这个程序,我得到了一个完全不同的错误.
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
using namespace std;
double dec2Bin(int value, char binaryString[])
{
int x = 1;
string hold = "";
while(x <= value){
x *= 2;
}
x /= 2;
while(x >= 1){
//cout << x << " ";
if(value > x){
hold += "1";
value -= x;
}
else if(value < x){
hold += "0";
}
else if(value == x){
hold += "1";
value = 0;
//return hold; …Run Code Online (Sandbox Code Playgroud) 我浏览了其他类似主题,但没有找到问题的答案......
下面的代码说明了这种情况.基类和派生类:
Base.hpp
namespace test {
template<class T>
class Base {
public:
Base();
virtual ~Base();
};
}
Run Code Online (Sandbox Code Playgroud)
Base.cpp
#include "Base.hpp"
namespace test {
template<class T>
Base<T>::Base() {
// TODO Auto-generated constructor stub
};
template<class T>
Base<T>::~Base() {
// TODO Auto-generated destructor stub
};
}
Run Code Online (Sandbox Code Playgroud)
Derived.hpp
namespace test {
class Derived : public Base<int> {
public:
Derived();
virtual ~Derived();
};
} /* namespace aeirtuaccess */
Run Code Online (Sandbox Code Playgroud)
Derived.cpp
#include "Derived.hpp"
namespace test {
Derived::Derived() {
// TODO Auto-generated constructor stub
};
Derived::~Derived() { …Run Code Online (Sandbox Code Playgroud) 我有几个 .h/.cpp 文件,每个文件都应该有名为“common.h / common.cpp”的文件,因为我在其余文件中重复使用其定义。common.h 文件有意定义了一些全局变量。
我在 Linux 和 gcc 4.4.7 下工作。
Compuler 工作正常,但 g++ 链接器在许多情况下抱怨,例如:
build/Debug/GNU-Linux-x86/StoreData2/spacewx.o:(.data+0x200): multiple definition of `nmdata::names`
build/Debug/GNU-Linux-x86/StoreData2/StoreData2.o:(.data+0x200): first defined here
Run Code Online (Sandbox Code Playgroud)
因此我没有成功构建。请建议如何消除此错误并保留这些全局变量。到目前为止,我只使用函数式编程(使用 C++ 库,例如 boost),没有自己的命名空间/对象。
我有一个g++ -std=c++17 -Wall -Og -g -o main *.cc完美编译的小代码.现在我想要有一个makefile,到目前为止我得到了这个:
CC = g++
CFLAGS = -Wall -std=c++17 -Og -g
DEPS = random_tools.h
OBJ = main.o random_tools.o
%.o: %.c $(DEPS)
$(CC) $(CFLAGS) -o $@ $<
main: $(OBJ)
gcc $(CFLAGS) -o $@ $^
Run Code Online (Sandbox Code Playgroud)
然而,当我跑make它崩溃时,告诉我
g++ -c -o main.o main.cc
g++ -c -o random_tools.o random_tools.cc
gcc -Wall -o main main.o random_tools.o
main.o: In function `main':
main.cc:(.text+0x1d): undefined reference to `std::cout'
main.cc:(.text+0x22): undefined reference to `std::ostream::operator<<(int)'
main.cc:(.text+0x27): undefined reference to `std::basic_ostream<char, std::char_traits<char> …Run Code Online (Sandbox Code Playgroud) 我试图定义ll为long long. 但是,这没有编译并引发错误。
我在 Windows 机器上使用 VS Code。我也在使用 gcc 8.2.0 版。
这是代码 -
#include <bits/stdc++.h>
using namespace std;
#define ll long long int;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin >> t;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在编译这个时,我收到了这个错误 -
test.cpp: In function 'int main()':
test.cpp:5:22: error: declaration does not declare anything [-fpermissive]
#define ll long long int;
^~~
test.cpp:12:5: note: in expansion of macro 'll'
ll t;
^~
test.cpp:12:8: error: 't' was not declared in this scope …Run Code Online (Sandbox Code Playgroud) 我想知道编译一个带有多个结果二进制文件的整个 C/C++ 项目的所有代码文件的路径。我知道https://github.com/rizsotto/Bear等人,您可以将其环绕在“make”调用中,并告诉您 gcc/g++ 调用,但它们当然只向我显示已编译的 C/C++ 文件- 我怎样才能找出编译需要哪些头文件并最终出现在生成的二进制文件之一中?
我创建了一个程序来反转一个句子:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string sentence;
string reversedSentence;
int i2 = 0;
cout << "Type in a sentence..." << endl;
getline(cin, sentence);
reversedSentence = sentence;
reverse(reversedSentence.begin(), reversedSentence.end());
cout << sentence << endl;
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试用 MinGW G++ 编译它时,会发生这种情况:
SentenceReverser.cpp: In function 'int main()':
SentenceReverser.cpp:16:5: error: 'reverse' was not declared in this scope
16 | reverse(reversedSentence.begin(), reversedSentence.end());
| ^~~~~~~
Run Code Online (Sandbox Code Playgroud)
我不知道我做错了什么。帮助?