【问题标题】:How do I compile with external header file in sublime text in C?如何在 C 中使用 sublime 文本中的外部头文件进行编译?
【发布时间】:2016-11-17 16:26:41
【问题描述】:

我在main.c 中使用了一个名为header.h 的标头。 功能测试在header.h 中公布,在test.c 中定义。 但是,即使我使用构建系统作为 C,它也会在下面说出这些话。

clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated
Undefined symbols for architecture x86_64:
  "test(int)", referenced from:
  _main in main-a4d82e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[Finished in 0.6s with exit code 1]
[cmd: ['bash', '-c', 'g++ -Wall -std=c++11 -O2 \'/Users/hanlock/Documents/CODE/TEST/TEST/main.c\' -o \'/Users/hanlock/Documents/CODE/TEST/TEST/main\' && osascript -e \'tell application "Terminal" to activate do script " cd \\"/Users/hanlock/Documents/CODE/TEST/TEST\\" &&start_ms=$(ruby -e \\"puts (Time.now.to_f * 1000).to_i\\")&&clear&&\\"/Users/hanlock/Documents/CODE/TEST/TEST/main\\" &&elapsed_ms=$(($(ruby -e \\"puts (Time.now.to_f * 1000).to_i\\") - start_ms))&& read -p \\"Press Enter to exit($elapsed_ms ms).\\"&&exit"\'']]
[dir: /Users/hanlock/Documents/CODE/TEST/TEST]
[path: /usr/bin:/bin:/usr/sbin:/sbin]

我在 C++ 中读过类似的问题。但是,它似乎不适用于我的情况。 所以,问题来了。如何在 C 语言中使用带有 sublime 的外部头文件?

代码如下:

main.c

#include <stdio.h>
#include "./Header.h"
int main(int argc, const char * argv[]) {
    test(1);
    return 0;
}

header.h

#ifndef Header_h
#define Header_h
int test(int);
#endif

test.c

#include <stdio.h>
int test(int i){
    printf("%d\n",i);
    return 0;
}

【问题讨论】:

  • 你能把你的代码和你用来编译它的命令包括进来吗?
  • 当然。已编辑。顺便说一句,它可以在 Xcode 中使用相同的代码。
  • 名字好像不匹配,是header叫head.h还是Header.h?
  • 哦,我编辑这个问题时弄错了。它没有出现在代码中。就像我说的,它可以在 Xcode 中运行。

标签: c sublimetext3


【解决方案1】:

在 test.c 中:

#include "header.h"

在你的外壳中:

gcc -I [dir] test.c

这将包括位于 dir 中名为 header.h 的任何外部头文件。 我希望这能回答你的问题。

【讨论】:

  • 对不起,“在你的外壳中”是什么意思。究竟是什么意思?
  • 看这里:stackoverflow.com/a/18100051/6523756 在cmd中,添加“-I”和你的目录。
  • 我试过了,还是不行。我尝试粘贴您推荐的代码或将 $file_name 更改为 test.c 或添加到 working_dir 的路径。他们只是没有工作。我对此有点困惑。很抱歉这么说,但我才刚刚开始学习如何使用它。
  • OK,在QuickFinder中,你输入iTerm,下载并安装相应的程序。运行。您将在 shell 上,这意味着您通过键入命令向您的计算机发出命令。使用命令“cd”转到 test.c 的位置。找到它后,输入“gcc test.c”(您可能需要安装 gcc)。如果你的 header 和 test.c 在同一个文件夹,它会自动包含它,否则你需要输入 gcc -I [pathtoheader] test.c
猜你喜欢
  • 1970-01-01
  • 2013-03-05
  • 2014-08-02
  • 2022-06-13
  • 2012-12-02
  • 1970-01-01
  • 2013-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多