小编pet*_*ohn的帖子

无法上传Github中要发布的文件

我想为我的Github项目创建一个版本,但是当我尝试将我的二进制文件(这是一个.zip文件)上传到该版本时,我收到以下错误消息:

出了点问题,我们无法处理该文件.

如果我尝试上传其他文件(例如我的自述文件),我会收到相同的错误消息.什么可能出错?

release github

12
推荐指数
3
解决办法
7746
查看次数

提升和自动提供

我正在制作一个使用Autoconf的项目.我有以下内容configure.ac:

AC_CHECK_HEADERS([boost/foreach.hpp], [],
    [AC_MSG_ERROR(You need the Boost libraries.)])
Run Code Online (Sandbox Code Playgroud)

当我运行时configure,它说它找不到这个头文件:

checking boost/foreach.hpp usability... no
checking boost/foreach.hpp presence... no
checking for boost/foreach.hpp... no
configure: error: You need the Boost libraries.
Run Code Online (Sandbox Code Playgroud)

这很奇怪,因为我有Boost.如果我删除了检查,代码编译,我安装了Boost:

$ find /usr/include -name foreach.hpp
/usr/include/boost/foreach.hpp
/usr/include/boost/test/utils/foreach.hpp
Run Code Online (Sandbox Code Playgroud)

请注意,我对SDL做了完全相同的工作.

AC_CHECK_HEADERS([SDL/SDL.h], [],
    [AC_MSG_ERROR(You need the SDL development library.)])
Run Code Online (Sandbox Code Playgroud)

...

checking SDL/SDL.h usability... yes
checking SDL/SDL.h presence... yes
checking for SDL/SDL.h... yes
Run Code Online (Sandbox Code Playgroud)

linux autoconf boost

11
推荐指数
3
解决办法
6086
查看次数

Valgrind在使用Boost线程时报告"可能丢失"内存

我有一个程序在一个单独的therad中运行一些动作,然后加入线程,比如这个:

#include <boost/thread.hpp>
#include <iostream>

using namespace std;

void f() {
    for (int i = 0; i < 100; ++i) cout << i << endl;
}

int main() {
    boost::thread t(f);
    t.join();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我在其上运行Valgrind,它会报告"可能丢失"的内存.如果省略join(),这似乎是合乎逻辑的,因为在这种情况下,当程序退出时线程仍在运行.但如果线程完成,我希望没有警告.

这是回溯:

==8797== 288 bytes in 1 blocks are possibly lost in loss record 2 of 3
==8797==    at 0x4A1F8B3: calloc (vg_replace_malloc.c:467)
==8797==    by 0x400F289: allocate_dtv (in /lib64/ld-2.4.so)
==8797==    by 0x400F34D: _dl_allocate_tls (in /lib64/ld-2.4.so)
==8797==    by 0x53EF981: pthread_create@@GLIBC_2.2.5 (in /lib64/libpthread-2.4.so)
==8797==    by 0x4B3311D: …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading valgrind g++ boost-thread

10
推荐指数
1
解决办法
932
查看次数

使用date_time_formatter时,使用Boost日志库进行编译错误

我正在尝试使用Boost日志库,我想在输出中添加时间戳.我从这个例子开始,但我遇到了编译错误.我完全按照示例复制了代码,并将init()函数更改为第二个(example_tutorial_formatters_stream_date_time).现在我有以下编译错误:

$ g++ -Wall -Wextra -O0 -g -isystem /proj/cudbdm/tools/external/boost/inst_1_54_0_cxx11/include -c main.cpp -o main.o
In file included from /proj/cudbdm/tools/external/boost/inst_1_54_0_cxx11/include/boost/log/expressions/formatters.hpp:23:0,
                 from /proj/cudbdm/tools/external/boost/inst_1_54_0_cxx11/include/boost/log/expressions.hpp:26,
                 from main.cpp:109:
/proj/cudbdm/tools/external/boost/inst_1_54_0_cxx11/include/boost/log/expressions/formatters/date_time.hpp: In instantiation of 'boost::log::v2s_mt_posix::expressions::format_date_time_terminal<T, FallbackPolicyT, CharT>::format_date_time_terminal(const boost::log::v2s_mt_posix::attribute_name&, const fallback_policy&, const string_type&) [with T = boost::posix_time::ptime; FallbackPolicyT = boost::log::v2s_mt_posix::fallback_to_none; CharT = char; boost::log::v2s_mt_posix::expressions::format_date_time_terminal<T, FallbackPolicyT, CharT>::fallback_policy = boost::log::v2s_mt_posix::fallback_to_none; boost::log::v2s_mt_posix::expressions::format_date_time_terminal<T, FallbackPolicyT, CharT>::string_type = std::basic_string<char>]':
/proj/cudbdm/tools/external/boost/inst_1_54_0_cxx11/include/boost/log/expressions/formatters/date_time.hpp:229:94:   required from 'boost::log::v2s_mt_posix::expressions::format_date_time_actor<AttributeValueT, boost::log::v2s_mt_posix::fallback_to_none, CharT> boost::log::v2s_mt_posix::expressions::format_date_time(const boost::log::v2s_mt_posix::attribute_name&, const CharT*) [with AttributeValueT = boost::posix_time::ptime; CharT = char]'
main.cpp:156:103:   required from here
/proj/cudbdm/tools/external/boost/inst_1_54_0_cxx11/include/boost/log/expressions/formatters/date_time.hpp:94:68: …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-log

10
推荐指数
1
解决办法
2244
查看次数

如何根据模板参数制作方法const?

我有一个带有模板参数的类,我想调用它的方法.它看起来像这样:

template <typename T>
class Foo {
public:
    void doSomething() {
        for (auto& t: ts) {
            t.doSomething();
        }
    }
private:
    std::vector<T> ts;
};
Run Code Online (Sandbox Code Playgroud)

这是有效的,但我想制作doSomething()const如果T它本身是const(假设它也是T::doSomething()const).我找到了一个可能的解决方案(基于这个问题),但我不喜欢它.

template <bool enabled = std::is_const<T>::value>
typename std::enable_if<enabled, void>::type
doSomething() const {
    for (auto& t: ts) {
        t.doSomething();
    }
}

template <bool enabled = !std::is_const<T>::value>
typename std::enable_if<enabled, void>::type
doSomething() {
    for (auto& t: ts) {
        t.doSomething();
    }
}
Run Code Online (Sandbox Code Playgroud)

它工作正常,但它有代码重复.有什么办法可以避免吗?

c++ const

10
推荐指数
1
解决办法
766
查看次数

bash中的变量递增

请考虑以下脚本:

#!/bin/bash

num=0
cat file | while read line; do
    echo "$line"
    lines[$num]="$line"
    ((num++))
    echo "num = $num"
done

echo "end num = $num"

i=0
while [ $i -lt $num ]; do
    echo "${lines[$i]}"
    ((i++))
done
Run Code Online (Sandbox Code Playgroud)

通常,它应该逐行读取文件,将结果存储在一个数组中,然后通过数组并逐行打印.问题是变量$ num在第一个循环退出后以某种方式重置.这个脚本的输出对我来说是(使用带有一些随机垃圾的文件):

dsfkljhhsdfsdfshdjkfgd
num = 1
fdfgdfgdfg
num = 2
dfgdfgdfgdfg
num = 3
dfgdfgdfgdfgdfg
num = 4
dfgdfgdfgdfgdfgd
num = 5
fgdfgdfgdfg
num = 6
dfgdfgdfgdfg
num = 7
dfgdfgdfgdfgdfg
num = 8
dfgdfgdfgdfg
num = 9
dfgdfgdgdgdg
num = 10
dfgdffgdgdgdg
num …
Run Code Online (Sandbox Code Playgroud)

linux variables bash

9
推荐指数
1
解决办法
2万
查看次数

阻止进程执行某些系统调用

我正在编写一个产生子进程的程序.出于安全原因,我想限制这些进程可以做什么.我知道程序之外的安全措施,chroot或者ulimit,但我想做更多的事情.我想限制子进程完成的系统调用(例如阻止调用open(),fork()以及这样的事情).有没有办法做到这一点?最理想的情况是,阻塞的系统调用应该返回错误,但如果不可能,则终止该进程也是好的.

我想它可以完成wuth ptrace()但是从手册页我真的不明白如何将它用于此目的.

c linux security system-calls

9
推荐指数
2
解决办法
2221
查看次数

向量和列表的end()迭代器的语义

根据C++标准,push_back()如果向量的新大小超过其容量,则调用向量会使迭代器无效,但在列表上它永远不会使迭代器失效.现在考虑以下代码片段:

1.

vector<int> v{1,2,3};
v.reserve(100);
for (int i: v) {
    v.push_back(i);
}
Run Code Online (Sandbox Code Playgroud)

2.

list<int> l{1,2,3};
for (int i: l) {
    l.push_back(i);
}
Run Code Online (Sandbox Code Playgroud)

我用gcc 4.8尝试过了,发现代码1点结束与v{1,2,3,1,2,3},但代码2个运行进入无限循环.对我来说,解释似乎很简单:指向内存位置的end()迭代器vector,并且因为它仅在基于循环的范围内计算一次,所以当它到达向量的第3个元素时它会停止.另一方面,list可能有一些null标记作为结束迭代器,它总是放在最后一个元素之后,因此循环永远不会到达它.

虽然结果看起来很简单,但我的问题是标准对此有何看法?它是否应该在每个标准库实现中都是如此,或者这种行为是否未定义?在编写一个可能调用push_back()这样一个容器的循环时我应该期待什么(我通常都想避免这种情况)?

c++ iterator c++11

9
推荐指数
1
解决办法
502
查看次数

如何有条件地将文件包含到Makefile中?

考虑以下Makefile:

# <include global configuration Makefile>

INCL = -I../include \
       -I<whatever>

CPPFLAGS=$(DEFS) $(INCL)
CXXFLAGS = -O0 -g -Wall -fmessage-length=0

SRCS = $(wildcard *.cpp)

OBJS = $(SRCS:.cpp=.o)

all: $(OBJS)

%.o: %.cpp
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<


depend: .depend

.depend: $(SRCS) 
    $(CPP) $(CPPFLAGS) -M $^ > $@

clean:
    rm -f $(OBJS)
    rm .depend


-include .depend
Run Code Online (Sandbox Code Playgroud)

此Makefile #include使用该g++ -M命令创建依赖关系链,并包含它.这可能是一个相当漫长的过程.问题是即使make clean被调用也会生成此文件,无论如何都会删除此文件.是否有条件地包含此文件,如果运行干净的目标,不打扰创建它?

c++ makefile

8
推荐指数
1
解决办法
3422
查看次数

如何禁用 ipython 中的代码格式?

IPython 有这个新功能,可以重新格式化我的提示符。不幸的是,它确实有问题,所以我想禁用它。我在从命令行启动 IPython 时成功做到了这一点,方法是在我的 中添加以下行ipython_config.py

c.TerminalInteractiveShell.autoformatter = None
Run Code Online (Sandbox Code Playgroud)

但是,当我从 python 脚本运行它时,它不起作用。我通过以下方式从脚本启动 IPython:

c = traitlets.config.get_config()
c.InteractiveShellEmbed.colors = "Linux"
c.TerminalInteractiveShell.autoformatter = None
c.InteractiveShellEmbed.loop_runner = lambda coro: loop.run_until_complete(coro)
IPython.embed(display_banner='', using='asyncio', config=c)
Run Code Online (Sandbox Code Playgroud)

如果我更改该colors值,颜色也会相应更改,因此配置本身可以正常工作。然而,无论我如何处理autoformatter,IPython 都会自动格式化我的代码。我究竟做错了什么?

python ipython

8
推荐指数
1
解决办法
276
查看次数