简体   繁体   English

在VScode、macOS中合并两个C程序文件

[英]Combine two C program files in VScode, macOS

I got two C program files, a basic code main.c and function.c我得到了两个 C 程序文件,一个基本代码 main.c 和 function.c

the function.c file contains the function which I need to call in main.c file i've tried merging files using -o the error appears to be : liker not found, although all the libraries and stuff are perfectly present in my system. function.c 文件包含我需要在 main.c 文件中调用的函数,我尝试使用 -o 合并文件,但错误似乎是:找不到liker,尽管所有库和东西都完美地存在于我的系统中。

I'am facing this problem in vsCode in macOS.我在 macOS 的 vsCode 中遇到了这个问题。

在此处输入图片说明

在此处输入图片说明

Let's begin with taking an example in the attachment there are two pictures if you see the picture which has code for one.c there I have used a line让我们从附件中的例子开始,如果你看到有 one.c 代码的图片,有两张图片,我用了一行


#include "two.c"

Just to tell the compiler that I want to see every single definition in two.只是告诉编译器我想将每个定义都分成两个。 c C


In file one.c I have called a function named print which is defined in two.在文件 one.c 中,我调用了一个名为 print 的函数,该函数定义为两个。 c C


One thing that can be noticed and is a really important observation is that in two.c , the file from where definitions needed to be called doesn't have a main function .可以注意到并且非常重要的一件事是two.c ,需要调用定义的文件没有 main function Can you guess why?你能猜到为什么吗? The reason is simply because when you click on one.c your system will call the main function located in one.c .原因很简单,因为当您单击one.c您的系统将调用位于one.c的 main 函数。 Upon seeing two main functions on the second file it gets confused and throws error, and just think why you even need the main function in two.c .在第二个文件中看到两个主要函数时,它会感到困惑并抛出错误,然后想想为什么你甚至需要two.c的主要函数。 There is no need because we use the main function just to perform certain actions and in two.c you really don't need to perform any action but you only need a function to be exported on another file.没有必要,因为我们使用 main 函数只是为了执行某些操作,而在two.c您实际上不需要执行任何操作,但您只需要将函数导出到另一个文件中。


In your case, you need to eliminate the main function from the function .c file and in main .c just include the path of your function .c在您的情况下,您需要从函数 .c 文件中删除 main 函数,而在 main .c 中只包含函数 .c 的路径

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

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