You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

树莓派Sense HAT使用libsensehat-c++库编译链接失败求助

树莓派Sense HAT C++开发编译问题解决

问题描述

在树莓派上连接Sense HAT并安装libsensehat-c库进行C开发,此前通过终端make命令可正常编译运行,当前遇到两个问题:

  • VS Code中使用g++编译时,出现大量函数未定义引用错误
  • 使用make命令时,因编译文件与实际代码文件不匹配,提示main函数未定义

示例代码

#include <iostream>
#include <sensehat.h>
#include <console_io.h>
#include <unistd.h>

using string = std::string;


int main() {
    if (senseInit()) {
        //Create 3 vars
        double sensitivity = 0.2;
        double x, y, z;
        double xBefore, yBefore, zBefore;
        senseGetAccelG(xBefore, yBefore, zBefore);

        int touchies = -1;
        while (true) {
            usleep(50000);
            //Directly Modifies xyz values. Returns bool
            xBefore = x;
            yBefore = y;
            zBefore = z;
            if (senseGetAccelG(x,y,z)) {
                if (x - xBefore >= sensitivity || y - yBefore >= sensitivity || z - zBefore >= sensitivity) {
                    std::cout << "DETECTED!!!! DONT TOUCH!!!!! STTOOOPP" << std::endl;
                
                    touchies++;
                    senseShowMessage("NO TOUCH");
                }
                else { std::cout << "NO MOVEMENT / Touches: " + std::to_string(touchies) + "\n" << std::endl; }
            }
            else {
                std::cout << "senseGetAccelG has failed" << std::endl;
            }
        }
        
    }  
    else {
        std::cout << "Something went wrong initating sense hat" << std::endl;
    }
    return 1;
}

g++编译错误信息

*  Executing task: C/C++: g++ build active file 

Starting build...
/usr/bin/g++ -fdiagnostics-color=always -g /home/fatblabs/projects/java/dontTouch.cpp -o /home/fatblabs/projects/java/dontTouch
/usr/bin/ld: /tmp/ccyHJQdC.o: in function `main':
/home/fatblabs/projects/java/dontTouch.cpp:10: undefined reference to `senseInit()'
/usr/bin/ld: /home/fatblabs/projects/java/dontTouch.cpp:15: undefined reference to `senseGetAccelG(double&, double&, double&)' 
/usr/bin/ld: /home/fatblabs/projects/java/dontTouch.cpp:24: undefined reference to `senseGetAccelG(double&, double&, double&)' 
/usr/bin/ld: /home/fatblabs/projects/java/dontTouch.cpp:29: undefined reference to `senseShowMessage(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)' 
/usr/bin/ld: /tmp/ccyHJQdC.o:/usr/local/include/sensehat.h:140: undefined reference to `senseGetRGBpixel(int, int)' 
/usr/bin/ld: /tmp/ccyHJQdC.o:/usr/local/include/sensehat.h:158: undefined reference to `senseSetRGBpixel(int, int, unsigned char, unsigned char, unsigned char)' 
/usr/bin/ld: /tmp/ccyHJQdC.o:/usr/local/include/sensehat.h:170: undefined reference to `senseGetRGBpixels()' 
/usr/bin/ld: /tmp/ccyHJQdC.o:/usr/local/include/sensehat.h:189: undefined reference to `senseSetRGBpixels(rgb_pixels_t)' 
/usr/bin/ld: /tmp/ccyHJQdC.o:/usr/local/include/sensehat.h:219: undefined reference to `senseShowRGBColoredLetter(char, rgb_pixel_t, rgb_pixel_t)' 
collect2: error: ld returned 1 exit status

Build finished with error(s).

make命令错误信息

fatblabs@raspberrypi:~/projects/java $ make
g++ -Wall -Wextra -Wduplicated-cond -Wduplicated-branches -Wsign-conversion -Wlogical-op -Wuseless-cast -Werror -pedantic -std=gnu++2a -o client client.cpp -lsensehat-c++ -lpng -li2c -lm -lRTIMULib -lgpiod
/usr/bin/ld: /usr/lib/gcc/aarch64-linux-gnu/12/../../../aarch64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x1c): undefined reference to `main'
/usr/bin/ld: (.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [Makefile:14: client] Error 1

解决方法

一、VS Code中g++编译链接库的配置

出现未定义引用错误的核心原因是编译时未链接libsensehat-c++库及其依赖库,VS Code默认编译任务未添加链接参数,需手动修改tasks.json:

  1. 打开VS Code,按下Ctrl+Shift+B打开任务面板,选择「创建tasks.json文件」,选择「Others」模板
  2. 将生成的tasks.json替换为以下配置,重点是在args数组末尾添加链接库参数:
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "g++ build with sensehat",
            "type": "shell",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-lsensehat-c++",
                "-lpng",
                "-li2c",
                "-lm",
                "-lRTIMULib",
                "-lgpiod"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": ["$gcc"]
        }
    ]
}
  1. 保存文件后,再次按下Ctrl+Shift+B即可完成带库链接的编译。

也可以直接在终端执行手动编译命令:

g++ -g dontTouch.cpp -o dontTouch -lsensehat-c++ -lpng -li2c -lm -lRTIMULib -lgpiod

二、make命令提示main函数未定义的修复

从错误信息可知,Makefile中指定编译的文件是client.cpp,但实际代码文件是dontTouch.cpp,导致找不到main函数。修复步骤:

  1. 打开项目目录下的Makefile,找到编译规则行,原内容类似:
client: client.cpp
	g++ -Wall -Wextra -Wduplicated-cond -Wduplicated-branches -Wsign-conversion -Wlogical-op -Wuseless-cast -Werror -pedantic -std=gnu++2a -o client client.cpp -lsensehat-c++ -lpng -li2c -lm -lRTIMULib -lgpiod
  1. 将所有client.cpp替换为实际文件名dontTouch.cpp,输出文件名client改为dontTouch,修改后如下:
dontTouch: dontTouch.cpp
	g++ -Wall -Wextra -Wduplicated-cond -Wduplicated-branches -Wsign-conversion -Wlogical-op -Wuseless-cast -Werror -pedantic -std=gnu++2a -o dontTouch dontTouch.cpp -lsensehat-c++ -lpng -li2c -lm -lRTIMULib -lgpiod
  1. 保存Makefile后,执行make命令即可正常编译。

内容的提问来源于stack exchange,提问作者Sean

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 09:14:52