简体   繁体   English

在 IAR linker 脚本 (.icf) 中使用 C/H 文件中的#define(预处理器值)

[英]Using #define (preprocessor values) from a C/H file in IAR linker script (.icf)

I need the usage of a certain #define value from a header into an icf file.我需要将 header 中的某个 #define 值用于 icf 文件。 Consider this,考虑一下,

I have a line in a certain header file like this.我在某个 header 文件中有这样一行。 #define LINKER_VALUE 0 #define LINKER_VALUE 0

If this LINKER_VALUE changes to 1, I have to do certain modifications in the icf file.如果这个 LINKER_VALUE 变成 1,我就得在 icf 文件中做一些修改。 But the visibility if this LINKER_VALUE does not seen in the linker script file.但是在 linker 脚本文件中没有看到这个 LINKER_VALUE 的可见性。

I have tried to include the header file from the script like this => include "example.h";我试过像这样从脚本中包含 header 文件 => include "example.h";

This successfully included the header file but it results in lot of errors.这成功包含了 header 文件,但会导致很多错误。 ( as it expects that header file to behave like a icf file ) (因为它期望 header 文件的行为类似于 icf 文件)

Is there any way to see LINKER_VALUE in the icf file?有什么办法可以在 icf 文件中看到 LINKER_VALUE 吗? Thanks.谢谢。

I solved this problem by using CMake for my projects.我通过为我的项目使用 CMake 解决了这个问题。

Adapting to your question, on the "CMakeLists.txt", I use:适应你的问题,在“CMakeLists.txt”上,我使用:

set(CMAKE_LINKER_VALUE 1)

configure_file(example.h.in example.h)
configure_file(config.icf.in config.icf)

So CMake propagates the configurations for headers and linker configurations.因此 CMake 传播标头和 linker 配置的配置。

Next, on the "example.h.in" header, the LINKER_VALUE becomes globally visible to the application:接下来,在“example.h.in”header 上, LINKER_VALUE对应用程序全局可见:

#pragma once
#define LINKER_VALUE @CMAKE_LINKER_VALUE@

And finally, on the "config.icf.in" linker configuration file, the same value is passed so the configuration change accordingly to certain needs.最后,在“config.icf.in”linker 配置文件中,传递了相同的值,因此配置会根据特定需求进行相应更改。 For example:例如:

define symbol LINKER_VALUE @CMAKE_LINKER_VALUE@
if (LINKER_VALUE > 0)
{
  /* specific configuration for this setup */
}

The CMake tutorial explains in detail how to use configure_file() . CMake 教程详细解释了如何使用configure_file()

The IAR tutorial explains how to build a CMake project. IAR 教程解释了如何构建一个 CMake 项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM