Fork me on GitHub

随笔 - 997  文章 - 5  评论 - 181  阅读 - 300万 

  今天在编译一个C代码的时候,从别的编译ok的头文件中拷贝了一份在上面做修改,没想到修改好之后一直

无法调用这个头文件中的函数和变量。看了好久,才在预编译宏中找到了问题的根源。代码

如下所示:

头文件A:

复制代码
#ifndef __I2S_LOOP_TEST_H__
#define __I2S_LOOP_TEST_H__

#ifdef __cplusplus
extern "C" {
#endif
int testa = 16;
#ifdef __cplusplus
}
#endif    
         
#endif 
复制代码

插入头文件B:

复制代码
#ifndef __I2S_LOOP_TEST_H__
#define __I2S_LOOP_TEST_H__

#ifdef __cplusplus
extern "C" {
#endif
int testb = 20;
#ifdef __cplusplus
}
#endif

#endif
复制代码

插入测试函数:

复制代码
#include <stdio.h>
#include "checka.h"
#include "checkb.h"

int main()
{

        testa = 1;
        testb = 3;
        printf("testa:%d testb:%d",testa,testb);

        return 0;
}
复制代码

运行结果:

test.c: In function ‘main’:
test.c:8:2: error: ‘testa’ undeclared (first use in this function)
  testa = 1;
  ^
test.c:8:2: note: each undeclared identifier is reported only once for each function it appears in

问题原因,是两个头文件中的预编译宏是一样的,会导致另外一个不会被运行。修改要给预编译宏即可。

posted on   虚生  阅读(237)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示