我学习了 C++ 文件系统库 ( std::filesystem) 的用法。以下代码导致链接器错误。
我该如何解决?
代码fs.cpp:
#include <iostream>
#include <filesystem>
#include <algorithm>
#include <iterator>
int main() {
using namespace std::filesystem;
directory_iterator iter{"."};
for (auto& ent: iter) {
std::cout << ent.path() << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
错误输出:
/usr/bin/ld: /tmp/cc7vhf9X.o: in function `main':
fs.cpp:(.text+0x10b): undefined reference to `std::filesystem::__cxx11::directory_iterator::operator*() const'
/usr/bin/ld: fs.cpp:(.text+0x151): undefined reference to `std::filesystem::__cxx11::directory_iterator::operator++()'
/usr/bin/ld: /tmp/cc7vhf9X.o: in function `std::filesystem::__cxx11::directory_iterator::directory_iterator(std::filesystem::__cxx11::path const&)':
fs.cpp:(.text._ZNSt10filesystem7__cxx1118directory_iteratorC2ERKNS0_4pathE[_ZNSt10filesystem7__cxx1118directory_iteratorC5ERKNS0_4pathE]+0x26): undefined reference to `std::filesystem::__cxx11::directory_iterator::directory_iterator(std::filesystem::__cxx11::path
const&, std::filesystem::directory_options, std::error_code*)'
/usr/bin/ld: /tmp/cc7vhf9X.o: in function `std::filesystem::__cxx11::path::path<char [2], std::filesystem::__cxx11::path>(char const (&) …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用也提供给我的 makefile 编译一些提供给我的代码。我在 Windows 10 上使用 MinGW 作为我的编译器。
我对 makefile 的经验很少,而且我创建和运行的 makefile 远没有我试图用来编译一些代码的那个复杂。
通常,当我使用 makefile 进行编译时,在命令提示符下,我会转到文件所在的目录,然后键入命令“make”并创建一个可执行文件,然后我就可以运行代码了。但是,这一次不起作用。我收到一条消息:
'make' 不是内部或外部命令,也不是可运行的程序或批处理文件。”
下面是make文件的内容:
CC=g++
CFLAGS=
INC=
LIB=
all: sorting
sorting: Driver.o Helper.o
$(CC) $(CFLAGS) -o $@ $^ $(LIB)
.cpp.o:
$(CC) $(CFLAGS) $(INC) -c -o $@ $^
Driver.cpp: Sorting.hpp Helper.cpp
Helper.cpp: Helper.hpp
clean:
rm -f *.o sorting
Run Code Online (Sandbox Code Playgroud)
我如何使用这个 make 文件来运行代码?
我正在编写一个涉及多个文件的示例.详细代码如下.
main.cpp中
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "grade.h"
#include "Student_Info.h"
using std::cin;
using std::cout;
using std::domain_error;
using std::endl;
using std::max;
using std::setprecision;
using std::sort;
using std::streamsize;
using std::string;
using std::vector;
int main()
{
vector<Student_Info> students;
Student_Info record;
string::size_type maxlen = 0; //the length of the longest name
//read and store all the student's data
//Invariant: students contains all the student records read so far
//maxlen contains the length of the …Run Code Online (Sandbox Code Playgroud) 我是用C++编写程序的新手,我想知道是否有办法将它导出为Windows格式.学习objective-c的好方法是什么,它与c ++有很大的不同吗?先感谢您.
我正在寻找system()C++程序返回-1 的原因和案例.我处于这样一种情况,它在一个不在另一个类中的类中工作正常.
我试图测试中的性能差异std::swap,并vector::swap和我有和没有编译-std=c++0x选项.我注意到大约200毫秒的差异,当我不包括这个选项时程序运行得更快.
#include <iostream>
#include <string>
#include <vector>
int main()
{
commentator.setReportStream (cout);
size_t nbElts = 2048;
vector<int> v, w;
v.resize (nbElts);
w.reserve (nbElts);
for (int i = 0; i < nbElts; ++i) {
w.push_back (i);
}
commentator.start ("std::swap", __FUNCTION__);
for (int i = 0; i < 10000000; ++i) {
std::swap (v, w);
}
commentator.stop (MSG_DONE);
commentator.start ("vector::swap", __FUNCTION__);
for (int i = 0; i < 10000000; ++i) {
v.swap (w);
}
commentator.stop (MSG_DONE);
return …Run Code Online (Sandbox Code Playgroud) 我得到以下代码,因为我试图了解getopt_long的用法.一切似乎都很好,但我得到"预期";' 在返回之前".我错过了什么?谢谢你们.
int next_option;
const string short_options = "a:bcde";
const struct option long_options[] =
{
{"op1", 1, NULL, 'a'},
{"op2", 1, NULL, 'b'},
{"op3", 1, NULL, 'c'},
{"op4", 0, NULL, 'd'},
{"op5", 0, NULL, 'e'},
{ NULL,0, NULL, 0}
};
do
{
next_option = getopt_long(argc,argv,short_options.c_str(),long_options,NULL);
switch(next_option)
{
case 'a':
cout <<" ";
break;
case 'b':
cout <<" ";
break;
case 'c':
cout <<" ";
break;
case 'd':
cout <<" ";
break;
case 'e':
cout <<" ";
break;
case '?': // …Run Code Online (Sandbox Code Playgroud) 可能重复:
未定义的引用
在我的目录中,我有:
main.cpp
tree.cpp
tree.h
Run Code Online (Sandbox Code Playgroud)
我在main.cpp中包含了tree.h
#include "tree.h"
Run Code Online (Sandbox Code Playgroud)
然后在我写的主要功能中
tree* t=new tree()
Run Code Online (Sandbox Code Playgroud)
为了编译我会做
g++ main.cpp
Run Code Online (Sandbox Code Playgroud)
但我有错误
undefined reference to `tree::tree()'
Run Code Online (Sandbox Code Playgroud)
有什么问题?
在使用g ++编译C/C++代码时,我对编译阶段有一个非常基本的了解,但我想要确认,澄清和额外的智慧珍珠.
对于这组文件:
main.c
foo.h
foo.c
bar.h
bar.c
Run Code Online (Sandbox Code Playgroud)
这些电话执行以下操作......
g++ -c foo.c
g++ -c bar.c
g++ -c main.c
Run Code Online (Sandbox Code Playgroud)
头文件现在添加到源文件中,所有这些.c文件都编译成.o文件.
g++ -o main.out main.o foo.o bar.o
Run Code Online (Sandbox Code Playgroud)
现在所有.o文件都链接在一起成为一个可执行文件 - main.out.
我写了一个c ++程序如下,
#include <iostream>
using namespace std;
main()
{
int a,b,c;
cout<<"Enter two numbers"<<endl;
cin>>a>>b;
c=a+b;
cout<<"Sum of two numbers are"<<c;
}
Run Code Online (Sandbox Code Playgroud)
并试图用以下程序编译程序,
home$ g++ add.cpp
Run Code Online (Sandbox Code Playgroud)
我收到这个错误,
程序g ++可以在以下包中找到,g ++,pentium-builder
有人能帮助我吗?
c++ ×10
g++ ×10
linux ×3
c ×2
c++11 ×1
c++17 ×1
gcc ×1
mingw ×1
objective-c ×1
performance ×1
pointers ×1
return ×1
system ×1
ubuntu-12.04 ×1