而不将其明确包含在项目中

而不将其明确包含在项目中

本文介绍了如何编译和链接其他源代码,而不将其明确包含在项目中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工具,在运行脚本部分的构建阶段生成额外的源代码。我想将本节的结果文件包含进编译和链接。怎么可能做呢?我知道可以在额外的运行脚本部分编写clang调用,但我正在寻找替代选项,因为它将太复杂,以保持运行脚本部分与clang和项目编译器设置同步。

I have a tool that generates additional source code on a build phase in a run script section. I would like to include resulting files of this section into compilation and linking. How it is possible to do? I know that it is possible to write clang calls in additional run script section but I am looking alternative options, since it will be too complex to keep run script section with clang and project compiler settings in sync.

我生成的文件是目前包含在项目中的类的一组类别。我不需要担心将类别导入到项目中,因为生成的所有代码都会自动导入generate-categories.h,默认导入到预编译头。

Files that I am generating is a set of categories to classes currently included into the project. I do not need to worry about importing categories into the project, since all the code that generated automatically imported into generated-categories.h which is imported by default into precompiled header.

推荐答案

您可以通过向您的项目添加一个包含以下内容的文件来解决此问题:

You can solve this problem by adding a file to your project that contains something like the following:

#include "generatedFile1.c"
#include "genreatedFile2.c"

等等。然后,你只需要使这个文件(或构建阶段)依赖(或运行)源代码生成步骤。

And so on. Then, you just need to make this file (or build phase) depend on (or run after) the source-code generating step.

我不是很熟悉Xcode,所以我不知道你会如何完成这一点;希望有更具体的经验的人能指出你在这方面的正确方向。

I'm not very familiar with Xcode, so I don't know specifically how you would accomplish that; hopefully someone with more specific experience can point you in the right direction on that front.

编辑:我在这里使用一个简单的项目。示例:

I made it work with a simple project here. Example:

main.m:

#import <Foundation/Foundation.h>
#import "generatedFile.m"

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        // insert code here...
        NSLog(@"%@", string);

    }
    return 0;
}

script.sh:

script.sh:

#!/bin/sh

echo "NSString *string = @\"Hello, World\";" > ${SYMROOT}/generatedFile.m

然后我添加了 $ {SYMROOT } ,并在编译源代码阶段之前添加了运行shell脚本阶段。

And then I added ${SYMROOT} in the "Header search paths" in the project settings and added a "run shell script" phase before the "compile sources" phase.

这篇关于如何编译和链接其他源代码,而不将其明确包含在项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 09:51