我的c ++项目有以下设置:
在src文件夹中,有*.cpp和相应的*.h文件,在obj文件夹中我想要我的.o文件.
到目前为止,编译和链接不是问题.但是现在我有太多.cpp和.h文件手动将它们放入Makefile中.所以我决定在makefile中编写这个小指令:
collect_sources:
@echo "collecting source files";
@echo "SRCS = \\" > sources;
@for file in ./src/*.cpp; \
do \
echo "$$file \\" >> sources; \
done
Run Code Online (Sandbox Code Playgroud)
我也这样做
-include sources
Run Code Online (Sandbox Code Playgroud)
在makefile的开头
生成的文件sources看起来很好,尽管在最后一行中有一个我不喜欢的反斜杠.但是afaik也应该是有害的.
我现在还需要自动构建依赖项.在我上一个版本中,我SRCS直接在Makefile中定义了*.cpp文件,以下代码很好:
$(SRCDIR)/%.cpp:
@$(CXX) $(DEPFLAGS) -MT \
"$(subst $(SRCDIR),$(OBJDIR),$(subst $(EXT_SRC),$(EXT_OBJ),$$file))" \
$(addprefix ,$$file) >> $(DEP);
clear_dependencies:
echo "" > $(DEP);
depend: clear_dependencies $(SRCS)
Run Code Online (Sandbox Code Playgroud)
但是包含sources-file,它永远不会到达上面的代码块.
以下是Makefile顶部定义的常量:
CXX = g++
CXXFLAGS = -Wall \
-Wextra \
-Wuninitialized \
-Wmissing-declarations \
-pedantic \
-O3 …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试在C中运行fork函数,在代码的子部分,我试图使用exacvp执行命令,但在执行之前我正在尝试一个永远不会执行的printf函数.我在调试中运行了这个,我注意到pid从未被分配0.我在一个单独的项目上尝试了一个简单的fork示例,它运行顺利.有没有人知道为什么孩子部分永远不会执行?
int startProcesses(int background) {
int i = 0;
while(*(lineArray+i) != NULL) {
int pid;
int status;
char *processName;
pid = fork();
if (pid == 0) {
printf("I am child");
// Child Process
processName = strtok(lineArray[i], " ");
execvp(processName, lineArray[i]);
i++;
continue;
} else if (!background) {
// Parent Process
waitpid(pid, &status, 0);
i++;
if(WEXITSTATUS(status)) {
printf(CANNOT_RUN_ERROR);
return 1;
}
} else {
i++;
continue;
}
}
return 0;
Run Code Online (Sandbox Code Playgroud)
}
我正在尝试使用共享库来构建模块化程序.这是我的小测试,展示了valgrind中糟糕的"仍然可以访问"的消息.
// module.h
#pragma once
struct Module
{
public:
Module(){}
virtual ~Module(){}
virtual int foo(const int, const double) = 0;
private:
Module(const Module&) = delete;
Module& operator=(const Module&) = delete;
};
Run Code Online (Sandbox Code Playgroud)
这是一个"模块"的接口,所有模块都应该继承,实现自己的版本foo.
实际实施:
// module_core.cpp
#include "module.h"
#include <iostream>
struct ModuleCore : public Module
{
int foo(const int a, const double b) override;
};
extern "C" Module* load()
{
return new ModuleCore;
}
extern "C" void unload(Module* module)
{
auto mc = static_cast<ModuleCore*>(module);
delete mc;
}
int ModuleCore::foo(const …Run Code Online (Sandbox Code Playgroud) 我想测试一个简单的事情,如下所示:
#include <iostream>
#include <boost/variant.hpp>
template<typename T1,typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1,T2>& dt){
os << dt.first << dt.second;
return os;
}
int main(){
boost::variant<int, std::pair<int,int>, bool> v;
v = std::pair<int,int>(3,3);
std::cout << v << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这实际上应该有效,因为对于普通类型,int, double等等,它会编译.
boost::variant有一个打印机vistor,它在内部使用它来输出内容到流.实际上这无法编译,但我真的不知道这个问题:
代码在这里失败:在variant_io.hpp中
template <typename OStream>
class printer
: public boost::static_visitor<>
{
private: // representation
OStream& out_;
public: // structors
explicit printer(OStream& out)
: out_( out )
{
}
public: // visitor interface
template <typename T>
void operator()(const T& …Run Code Online (Sandbox Code Playgroud) #include <iostream>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
struct node {
int number;
string name;
node* next;
};
int hash(string dna) {
int i, sum=0;
for (i=0; i<dna.size(); i++) {
if (dna[i]=='A') {
}
else if (dna[i]=='C') {
sum+=1;
}
else if (dna[i]=='G') {
sum+=2;
}
else {
sum+=3;
}
}
cout << "Hash sum is " << sum << endl;
return sum;
}
int main() {
//input
int dnalength, sublength;
cin >> dnalength >> sublength;
string …Run Code Online (Sandbox Code Playgroud) 我有一个非常大的std :: vector std :: vectors,它包含固定数量的无符号整数.
所有uint向量都按升序排序.
我目前消除重复向量的方法是
unsigned int i = 0;
while ( i < new_combs.size() )
{
unsigned int j = i + 1;
while ( j < new_combs.size() )
{
unsigned int k = 0;
while ( k < new_combs.at(i).size() && new_combs.at(i).at(k) == new_combs.at(j).at(k) )
++k;
if ( k == new_combs.at(j).size() )
new_combs.erase(new_combs.begin() + j);
else
++j;
}
++i;
}
Run Code Online (Sandbox Code Playgroud)
这里,new_combs是包含如上所述的向量的向量.
如果向量矢量未排序,是否有更有效的方法来消除重复?
我编写了下面的代码来从文本文件中读取数据并将值存储在数组中.我的代码没有读取文件.
正在使用的库头文件
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <conio.h>
Run Code Online (Sandbox Code Playgroud)
我的主要
int main()
{
FILE *fpoo;
float NumArr[5];
Run Code Online (Sandbox Code Playgroud)
//我同样试过"c:\ Fly_u.txt"
fpoo= fopen ("Fly_u.txt","r");
if(fpoo!=NULL)
for (int i=0;i<6;i++)
{
fscanf(fpoo,"%d\n",NumArr[i]);
{
else
{
printf("me");
}
for (int i=0;i<6;i++)
{
printf("%f",NumArr[i]);
}
fclose(fpoo);
_getche();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
//文本文件的内容如下
0.99247
0.14727
-0.00041387
-1.737
0.20475
-0.052151
0.14755
-0.0233
-0.32606
0.092085
0.059199
-0.025587
0.0097565
0.13639
0.12007
Run Code Online (Sandbox Code Playgroud) 我创建了一张地图:
map<int, int> mapOriginal;
Run Code Online (Sandbox Code Playgroud)
在填充数据后,看起来像这样:
mapOriginal[0] = 3;
mapOriginal[1] = 2;
mapOriginal[2] = 1;
Run Code Online (Sandbox Code Playgroud)
而mapOriginal ouptut是:
0-> 3
1-> 2
2-> 1
Run Code Online (Sandbox Code Playgroud)
我想创建另一张地图:
map<int, int> mapReverse;
Run Code Online (Sandbox Code Playgroud)
但是如何将mapOriginal值反转为mapReverse:
mapReverse ouptut将是:
0-> 1
1-> 2
2-> 3
Run Code Online (Sandbox Code Playgroud) 我是新手,C++但我需要以多线程方式编写代码.在下面的代码中,如果Increase_value()由多个线程调用,那么确保Print_Value()打印最初增加的ID值的最有效方法是什么Increase_value().换句话说,如何确保在运行期间Print_Value()打印一系列数字而不会遗漏任何值(数字的顺序不是问题).可以使用C++11帮助中讨论的原子变量吗?
class foo
{
public:
static int ID;
void Increase_value()
{
ID++;
Print_Value (ID-1);
}
void Print_Value(int k)
{
cout << k ;
}
};
Run Code Online (Sandbox Code Playgroud) 我有两个头文件.decimal.h和integer.h,每个都包含它们各自的类.我想写这样的东西.
//integer.h
#ifndef INTEGER_H
#define INTEGER_H
#include "decimal.h"
class Integer
{
...
operator Decimal();
}
#endif
//decimal.h
#ifndef DECIMAL_H
#define DECIMAL_H
#include "integer.h"
class Decimal
{
...
operator Integer();
}
#endif
Run Code Online (Sandbox Code Playgroud)
给我带来麻烦的是,因为它们包含了它,所以它在Visual Studio中表现得很奇怪并且产生奇怪的编译器错误.有没有办法解决?