VScode中C++代码有波浪线提示错误,但编译正常
今天在写个小demo的时候遇到了一个奇葩的问题。我的代码可是,在VScode编辑中,加入#include <pcl_conversions/pcl_conversions.h>这句话,就会出现namespace “ros” has no member “init” namespace “ros” has no member "NodeHandle"之类的错误提示,当然,这些提示并不影响我编译代码,
今天在写个小demo的时候遇到了一个奇葩的问题。
我的代码
/**
* @file main.cpp
* @author Xiaochen Wang (xchwang@whu.edu.cn)
* @brief test for fast cluster the range image of las
* @version 0.1
* @date 2025-02-26
*
* @copyright Copyright (c) 2025
*
*/
#include <pcl_conversions/pcl_conversions.h>
// ROS
#include <ros/ros.h>
#include <std_msgs/String.h>
#include <sensor_msgs/PointCloud2.h>
// parse the parameters
#include "tclap/CmdLine.h"
// the useful functions
#include "utils/colors.h"
#include "utils/tictoc.h"
#include "utils/DST.h"
int main(int argc, char* argv[]) {
// read the parameters
TCLAP::CmdLine cmd(
"Subscribe to /ouster topic and show clustering on the data.",
' ', "v1.0");
TCLAP::ValueArg<int> angle_arg(
"", "angle",
"Threshold angle. Below this value, the objects are separated", false, 10,
"int");
TCLAP::ValueArg<int> num_beams_arg(
"", "num_beams", "Num of vertical beams in laser. One of: [16, 32, 64, 128].",
true, 0, "int");
cmd.add(angle_arg);
cmd.add(num_beams_arg);
cmd.parse(argc, argv);
std::cout << BOLDBLUE << "num_beams_arg.getValue(): " << num_beams_arg.getValue() << RESET << std::endl;
// 初始化节点,名称必须唯一
ros::init(argc, argv, "simple_publisher");
ros::NodeHandle nh;
// 创建发布者,话题名为"/chat",队列大小10
ros::Publisher pub = nh.advertise<std_msgs::String>("/chat", 10);
// 设置发布频率 (1Hz)
ros::Rate rate(1);
int count = 0;
while (ros::ok()) {
std_msgs::String msg;
msg.data = "Hello ROS! Count: " + std::to_string(count++);
// 发布消息
pub.publish(msg);
// 打印日志
ROS_INFO("[Publisher] Sent: %s", msg.data.c_str());
rate.sleep();
}
return 0;
}
编译过程中没有任何错误,也能够正常运行。头文件都是可以跳转的。
可是,在VScode编辑中,加入#include <pcl_conversions/pcl_conversions.h>这句话,就会出现namespace “ros” has no member “init” namespace “ros” has no member "NodeHandle"之类的错误提示,当然,这些提示并不影响我编译代码,只是在编辑器中显示有错误。
当我注释了#include <pcl_conversions/pcl_conversions.h>之后,这些错误提示就会消失。
我以为需要设置c_cpp_properties.json文件中的内容,所以修改为如下:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/opt/ros/melodic/include/**",
"${workspaceFolder}/src/fast_cluster_test/include/**",
"/usr/include/pcl-1.8/**",
"/usr/include/**",
"/usr/local/include"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
我觉得这些内容都是没有错误的,我不知道为什么#include <pcl_conversions/pcl_conversions.h>这句话能够影响VScode编辑器的错误提示。
网上查了很多方案都没有解决,最后使用CMake在构建项目时生成的compile_commands.json文件(其中,包含了所有编译单元的具体编译命令。这些命令包括编译器路径、头文件搜索路径、宏定义等。)解决问题。
通过两个步骤:
-
- CmakeLists文件中,添加设置 set(CMAKE_EXPORT_COMPILE_COMMANDS ON)会生成compile_commands.json文件
-
- 在c_cpp_properties.json文件中添加一行命令,指定到compile_commands.json文件就可以了
"compileCommands": "${workspaceFolder}/build/compile_commands.json"
更多推荐



所有评论(0)