我使用 C++ 已经很长时间了,但我来自 Windows。
我接到了一个使用 CMake 的项目。我用谷歌搜索试图学习它。
我运行cmake .
我假设它总是寻找CMakeLists.txt并生成makefile。
这会创建更多的 cmake 文件和 make 文件。然后我被指示跑make package。我假设package只是一个可以是任何东西的目标名称。
然后我收到错误:
c++: error: unrecognized command line option ‘-mthumb-interwork’
c++: error: unrecognized command line option ‘-mfloat-abi=hard’
c++: error: unrecognized command line option ‘-mfpu=neon’
Run Code Online (Sandbox Code Playgroud)
我认为这是因为我正在尝试针对与我所使用的架构不同的架构进行编译。我假设所有的 cmake 文件都会为我正确设置。我还假设没有什么是专有的。
我在 CMakeLists.txt 中看到了这一行
SET(CMAKE_CXX_FLAGS "-march=armv7-a -mthumb-interwork -mfloat-abi=hard -mfpu=neon -mtune=cortex-a9 -std=c++11 -O3")
Run Code Online (Sandbox Code Playgroud)
如果我查看官方 cmake 文档,它会说,并且我引用了 “所有构建类型的标志”。....不是很有帮助
我认为它没有使用正确的编译器,但我也看不到 CMakeLists.txt 中编译器的指定位置。
问题是:为什么这些标志无法识别?
例如:
有3个源文件{ main.c test1.c test2.c}在目录中,命名的目录文件test3,并且有一个名为源文件test.c中的目录test3.
现在我想创建一个makefile来编译和链接这四个源文件.
这是我的Makefile:
# Cancel statement "CC=gcc"
src:=$(wildcard *.c) test3.c
obj:=$(patsubst %.c,%.o,$(src))
main:$(obj)
gcc -o main $(obj)
.PHONY:clean
clean:
rm *.o *~
Run Code Online (Sandbox Code Playgroud)
当我打电话make来编译它们时,我得到了这样的输出:
cc -c -o main.o main.c
cc -c -o test1.o test1.c
cc -c -o test2.o test2.c
cc -c -o test3.o test3/test3.c
gcc -o main main.o test1.o test2.o test3.o
Run Code Online (Sandbox Code Playgroud)
我知道'cc'在Linux中与'gcc'相关联.
我不明白为什么Make调用cc编译这四个源文件,但调用gcc链接目标文件?
printf("%.3lf\n", -0.0001);
Run Code Online (Sandbox Code Playgroud)
这输出-0.000,但不应该是0.000?
如何在没有减号的情况下进行打印,即0.000?
我正在尝试使用该功能:
bool pcl::visualization::PCLVisualizer::addPointCloud(const pcl::PointCloud<pcl::PointXYZ >::ConstPtr & cloud,
const std::string & id = "cloud",
int viewport = 0
)
Run Code Online (Sandbox Code Playgroud)
所以我有以下代码作为全局变量:
pcl::PointCloud<pcl::PointXYZ> linaccpc;
const pcl::PointCloud<pcl::PointXYZ> * linptr = &linaccpc;
boost::shared_ptr<pcl::visualization::PCLVisualizer> linplotter (new pcl::visualization::PCLVisualizer("linear acceleration");
Run Code Online (Sandbox Code Playgroud)
然后在我的主要功能中:
linplotter->addPointCloud<pcl::PointXYZ> (linptr, "linear acc", 0);
Run Code Online (Sandbox Code Playgroud)
我收到错误:
error: no matching function for call to 'pcl::visualization::PCLVisualizer::addPointCloud(const pcl::PointCloud<pcl::PointXYZ>*&, const char [11], int)'
linplotter->addPointCloud<pcl::PointXYZ> (linptr, "linear acc", 0);
^
Run Code Online (Sandbox Code Playgroud)
任何帮助深表感谢。
我正在学习使用指针的方法std::unique_ptr.我的代码:
#include <iostream>
#include <memory>
class object{
public:
void say(){
std::cout<<"hi";
}
};
int main(){
std::unique_ptr<object> p =
std::unique_ptr<object>(new object);
p->say();
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:
我使用std::unique_ptr得当吗?
如何删除或删除this(p)指针,以便没有内存使用或泄漏?
我正在使用yocto。它不支持基于ft5x06s的触摸屏,因此我决定添加一个补丁。但是,当我添加补丁文件时,出现以下错误:
ERROR: Command Error: exit status: 1 Output:
Applying patch 0026-imx6q-smx6-edt-ft5x06.patch
patching file Documentation/devicetree/bindings/input/touchscreen/edt-ft5x06.txt
patching file drivers/input/touchscreen/edt-ft5x06.c
Hunk #22 FAILED at 751.
Hunk #23 succeeded at 811 (offset -1 lines).
Hunk #24 FAILED at 922.
Hunk #25 FAILED at 959.
Hunk #26 FAILED at 995.
Hunk #27 FAILED at 1009.
Hunk #28 succeeded at 1077 (offset 17 lines).
5 out of 28 hunks FAILED -- rejects in file drivers/input/touchscreen/edt-ft5x06.c
Patch 0026-imx6q-smx6-edt-ft5x06.patch does not apply (enforce with -f)
ERROR: Function failed: …Run Code Online (Sandbox Code Playgroud) 我有一个带有两个typename参数的模板方法(实际上是QObject::connect()- 看到这个答案和另一个答案).因为类型名是针对成员指针的,所以当传入的名称引用重载函数时,演绎可能会失败; 当发生这种情况时,我们需要将一个参数强制转换为正确的类型(可能通过将其存储到所需类型的局部变量中)或者使用一个或多个模板参数限定调用.
以一个相关问题为例:
QObject::connect(spinBox, &QSpinBox::valueChanged,
slider, &QSlider::setValue);
Run Code Online (Sandbox Code Playgroud)
需要写成
QObject::connect<void(QSpinBox::*)(int)>(spinBox, &QSpinBox::valueChanged,
slider, &QSlider::setValue);
Run Code Online (Sandbox Code Playgroud)
或(通过胁迫):
void(QSpinBox::*signal)(int) = &QSpinBox::valueChanged;
QObject::connect(spinBox, signal,
slider, &QSlider::setValue);
Run Code Online (Sandbox Code Playgroud)
但是,有时可以推导出第一个模板参数,但后面的参数是必需的.是否有一种简单的方法来默认第一个参数,但是指定其他参数?我在想类似的东西
QObject::connect<auto, void(QSpinBox::*)(int)>(slider, &QSlider::valueChanged,
spinBox, &QSpinBox::setValue);
Run Code Online (Sandbox Code Playgroud)
显然,这不是有效的C++,但我希望它说明了这一点.
我知道我可以写
void(QSpinBox::*slot)(int) = &QSpinBox::setValue;
QObject::connect(slider, &QSlider::valueChanged,
spinBox, slot);
Run Code Online (Sandbox Code Playgroud)
但我希望有一个更简洁的语法.
有一个函数需要exit(1)特定的情况,我正在尝试使用gtest对该函数运行单元测试.
我以为我可以在函数调用的情况下使用try/catch exit(1),但它并不适合我.
这是正常的行为吗?如果是这样,我怎么能检测到exit(1)被调用?
我只是想了解管道,我正在研究一个例子:
#define _XOPEN_SOURCE 700
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
pid_t p;
int status;
int pipefd[2];
pipe(pipefd);
p = fork();
if(p == 0)
{
dup2(pipefd[1], 1);
close(pipefd[0]);
close(pipefd[1]);
execlp(argv[1], argv[1], NULL);
perror(argv[1]);
exit(1);
}
dup2(pipefd[0], 0);
close(pipefd[0]);
close(pipefd[1]);
execvp(argv[2], argv+2);
perror(argv[2]);
return 1;
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么这段代码在使用之前关闭了pipefd.
为什么在这里关闭?
我在使用时遇到段错误std::unordered_map::emplace()。这是最小的可重现示例:
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
class WordTable {
public:
WordTable() {
total = 0;
}
~WordTable() {}
void addWord(const string word, const int incr = 1) {
cout << "begin emplace" << endl;
table.emplace(word, Node()); //this is where the seg fault occurs
cout << "emplace succeeded" << endl;
if (incr) {
table[word].incrementCount();
incrementTotal();
}
}
private:
struct Node {
public:
Node() {
count = 0;
kids = new WordTable();
}
~Node() {
delete …Run Code Online (Sandbox Code Playgroud)