小编pet*_*ohn的帖子

在C++中编译时获取类型名称

我想获取类型名称并将其打印用于调试目的.我使用以下代码:

#include <cxxabi.h>

inline const char* demangle(const char *s) {
    abi::__cxa_demangle(s, 0, 0, NULL);
}

template<typename T>
inline const char* type_name() {
    return demangle(typeid(T).name());
}
Run Code Online (Sandbox Code Playgroud)

它运行良好,但我认为有一个不必要的运行时开销.有没有办法获得在编译时计算的类型id的人类可读形式?我在考虑这样的事情:

boost::mpl::type_name<MyType>::value
Run Code Online (Sandbox Code Playgroud)

哪个会返回类型名称的字符串常量.

作为(不是那么严格)相关的问题:是否可以使用boost :: mpl进行字符串处理?

c++ boost typeinfo boost-mpl

4
推荐指数
1
解决办法
4744
查看次数

"make clean"不会清除Automake的依赖关系吗?

我有一个使用Autoconf和Automake的C++项目.我决定在我的src目录中有太多源文件,并将一些文件移动到子目录.然后,我修改了Makefile.am这些文件并将其更改source.cppsubdir/source.cpp.我知道这种方法应该可行,就像我之前为一些新文件做的那样(但后来我没有重命名).现在我像往常一样运行以下内容:

autoreconf
./configure
make clean
make
Run Code Online (Sandbox Code Playgroud)

我收到了类似这样的错误消息:

No rule to make target "source.cpp" needed for "source.o"
Run Code Online (Sandbox Code Playgroud)

我不明白出了什么问题.我检查了我的Makefile,但它似乎是正确的.所以我将我的git存储库克隆到了一个新的地方,然后在那里尝试了一个make,它运行起来了.没问题,我想,并git clean -xf在我的原始目录上做了一个.在此之后,编译仍然无法正常工作.现在我在两个目录结构上做了一个差异(在另一个目录结构之后git clean -xf,发现还有一个.deps目录.删除之后,它编译了.

故事的寓意如下:

  • make clean 不会删除依赖项.
  • git clean -xf 不删除依赖项(可能是因为隐藏目录).

是否有任何方法可以自动删除此目录make clean(或可能git clean)?当然我可以手动完成,但是在清理后还有依赖文件是非常烦人的.

git automake dependencies makefile

4
推荐指数
2
解决办法
4315
查看次数

Linux程序中的路径管理

我有一个新手Linux编程问题.假设我有一个使用自动工具来编译和部署一个项目,我有数据文件,这些文件将被安装在类似的位置/var/something/usr/share/something等,但在Autoconf的,我可以改变这些安装路径.程序应该如何找到这些文件?它是如何知道它们实际安装的位置(如果在任何地方,因为程序即使没有安装也应该工作,但是从它的构建位置运行)?

linux autotools

3
推荐指数
1
解决办法
110
查看次数

如何在Perl中有条件地导入包?

我有一个Perl脚本,它使用一个不常见的模块,我希望它可以在没有安装该模块的情况下使用,尽管功能有限.可能吗?

我想到了这样的事情:

my $has_foobar;
if (has_module "foobar") {
    << use it >>
    $has_foobar = true;
} else {
    print STDERR "Warning: foobar not found. Not using it.\n";
    $has_foobar = false;
}
Run Code Online (Sandbox Code Playgroud)

import perl module

3
推荐指数
1
解决办法
1031
查看次数

在vim中重新映射“Y”

vim 中的复制粘贴功能对我来说似乎有点不一致。命令yy,ddccyank/delete 整行。命令DC删除从光标到行尾,而是Y猛拉整行。我想和和Y一样工作。所以我把下面这行放在我的:DC.vimrc

nmap Y y$
Run Code Online (Sandbox Code Playgroud)

虽然它似乎不起作用。我的第一个想法是这是因为一些插件干扰。我试图将命令放在 my 的开头和结尾.vimrc,但没有任何帮助。但是,如果我手动输入命令(不是来自.vimrc),它会起作用。为什么是这样?我如何使这项工作?

vim key-bindings

3
推荐指数
1
解决办法
1529
查看次数

Boost序列化不适用于shared_ptr <int>

以下代码编译得很好:

#include <boost/serialization/shared_ptr.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <sstream>
#include <memory>

struct A {
    int i;

    A(): i(0) {}
    A(int i): i(i) {}

    template <typename Archive>
    void serialize(Archive& ar, const unsigned int) {
        ar & i;
    }
};

int main() {
    auto a = std::make_shared<A>(465);
    std::stringstream stream;
    boost::archive::text_oarchive out{stream};
    out << a;
}
Run Code Online (Sandbox Code Playgroud)

现在我希望如果我替换Aint那么它也应该工作.

#include <boost/serialization/shared_ptr.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <sstream>
#include <memory>

int main() {
    auto a = std::make_shared<int>(465);
    std::stringstream stream;
    boost::archive::text_oarchive out{stream};
    out …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-serialization

3
推荐指数
1
解决办法
347
查看次数

修复dlopen和dlcose的未定义引用

我创建了简单的c ++应用程序.我可以编译它并且工作正常.但现在我需要动态加载库,我已经将dlfnc.h添加到我的项目中并添加了一些代码:

#include <iostream>
#include <dlfcn.h>
void *mylib;
int eret;

using namespace std;

int main() {

    mylib = dlopen("mylib.so", RTLD_LOCAL | RTLD_LAZY);
    eret = dlclose(mylib);

    cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译:

myname@ubuntu:~/workspace/LinuxGcc/src$ g++ LinuxGcc.cpp 
Run Code Online (Sandbox Code Playgroud)

并得到编译错误:

/tmp/ccxTLiGY.o: In function `main':
LinuxGcc.cpp:(.text+0xf): undefined reference to `dlopen'
LinuxGcc.cpp:(.text+0x25): undefined reference to `dlclose'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

dlfcn.h 存在于 /usr/include/

哪里有问题?

c++ linux g++ dlopen

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

如何设置 Mosquitto 桥接配置?

我想设置 Mosquitto 的测试配置。对于一个配置文件,我只写了这个:

listener 1883
Run Code Online (Sandbox Code Playgroud)

我用这个配置文件启动了一个服务器,它启动成功。

第二个配置文件有以下内容:

listener 1884
connection test
address 127.0.0.1:1883
Run Code Online (Sandbox Code Playgroud)

当我尝试用这个启动服务器时,我收到以下错误消息:

Error: Invalid bridge configuration.
Error: Unable to open configuration file.
Run Code Online (Sandbox Code Playgroud)

无论我在运行或不运行其他服务器的情况下尝试它,还是尝试外部地址或本地地址都没有关系。

mqtt mosquitto

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

如何打开垂直分割下方的quickfix窗口?

我通常使用几个垂直拆分,以便每个拆分的宽度不超过80个字符(我处理的代码倾向于遵循80列的限制)。但是,对于快速修复程序或位置窗口而言,这通常是不够的。有没有办法像这样在所有垂直裂缝下方打开它?

+----+----+----+
|    |    |    |
|    |    |    |
|    |    |    |
|    |    |    |
+----+----+----+
| quickfix     |
+--------------+
Run Code Online (Sandbox Code Playgroud)

vim

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

如何在Android中定位ListView?

我想ListView在我的活动中放置一个顶部,在底部放置其他东西,而不是ListView"吞下"底部的小部件?我试过这个("其他东西"只有一个RelativeLayout按钮),但它不起作用.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" >

    </ListView>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="bottom" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:text="Button" />

    </RelativeLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在这里寻找了一些提示,这非常有用,但没有提及ListViews.我可以将小部件置于上面ListView而不是问题,但不能在下面.

android android-layout android-listview

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

$ {var:-word}和$ {var-word}之间有什么区别?

我在bash脚本中找到了以下命令:

git blame $NOT_WHITESPACE --line-porcelain "${2-@}" -- "$file"
Run Code Online (Sandbox Code Playgroud)

${2-@}是什么意思?尝试,它返回第二个参数,如果它不存在则返回"@".根据文档,${2:-@}应该做同样的事情.我试了一下,确实也是这样.有什么不同?它在哪里记录?手册页似乎没有说明这种表示法.

parameters bash

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

异常被推迟了

我在Python中有以下脚本:

import sys


for filename in sys.argv:
    with open(filename, mode='r') as f:
        print("Foobar")
Run Code Online (Sandbox Code Playgroud)

如果我使用不存在的文件名作为参数启动脚本,我会得到一个异常,如预期的那样.但是,print()即使我不希望它仍在执行中.

Foobar
Traceback (most recent call last):
  File "home/bin/ksp-increment-build", line 16, in <module>
    with open(filename, mode='r') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'asdads'
Run Code Online (Sandbox Code Playgroud)

为什么是这样?

python exception

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

为什么我不能在C++中使用模板化的typedef?

考虑以下程序:

#include <iostream>
#include <algorithm>

using namespace std;

template<class T>
struct A {
    typedef pair<T, T> PairType;
};

template<class T>
struct B {
    void f(A<T>::PairType p) {
        cout << "f(" << p.first << ", " << p.second << ")" << endl;
    }
    void g(pair<T, T> p) {
        cout <<"g(" << p.first << ", " << p.second << ")" << endl;
    }
};

int main() {
    B<int> b;
    b.f(make_pair(1, 2));
    b.g(make_pair(1, 2));
}
Run Code Online (Sandbox Code Playgroud)

为什么不编译?它抱怨该B::f()方法的部分.它似乎没有认识到类中的typedef A<T>.如果我改为T具体类型,它可以工作.完整的错误消息如下:

g++ …
Run Code Online (Sandbox Code Playgroud)

c++ templates typedef

0
推荐指数
1
解决办法
216
查看次数