【问题标题】:Trying to add my own header files just as C library header files are added to the program containing MAIN using gcc尝试添加我自己的头文件,就像使用 gcc 将 C 库头文件添加到包含 MAIN 的程序一样
【发布时间】:2014-05-04 08:29:46
【问题描述】:

这里有三个不同的文件。我正在尝试添加myfunc.h,就像将C库头文件添加到包含main的程序中一样,这样myfunc.h中存在的函数就可以通过使用#include<myfunc.h>来调用.


myfunc.c

float power(float p, float q)
{
  float r,prod;
  int i;
  prod=p;
  for(i=1;i<=q-1;i++)
  prod=prod*p;
  return prod;
}


char prime(int n)
{
  int i;
  for(i=2;i<=n-1;i++)
  {
    if(n%i==0)
    return 'N';
  }
  if(n==i)
  return 'Y';
}



myfunc.h

float power(float p, float q);
char prime(int n);



Practice.c 包含main()

#include <stdio.h>
#include "myfunc.h"

main()
{
  int i,k;

  printf("\nEnter Number = ");
  scanf("%d",&i);

  k=prime(i);

  printf("\n  IS %d PRIME = %c",i,k);
}

问题:1如何在我的 中使用 myfunc.cmyfunc.h main 程序?

问题:2我创建并编译了 myfunc.h,它反过来生成了 myfunc.h.gch 文件。这是什么myfunc.h.gch


考虑 Example 计划。

#include <stdio.h>

main()
{
  printf("This is an Example.");
}

我是否在 main() 或任何地方声明或定义了 printf()?不,我只是用传入的值调用它。但是程序仍然可以成功编译和执行。

这正是我希望 myfunc.c 中的函数被调用的方式。我该怎么做?

【问题讨论】:

  • 尚不清楚您有什么具体问题。您已经使用您应该使用的标准、广泛使用的技术成功地编译了您的程序。这里没有任何问题。就这样继续下去。
  • “在此之后,我创建并编译了 myfunc.h,然后生成了 myfunc.h.gch”。这个不是很广泛使用。您已经在此处构建了一个预编译的头文件。我建议不要使用预编译头文件,除非您有一个大型项目,否则编译时间太长。您可能不希望与它们相关的任何额外复杂性。
  • @n.m.我在问题中编写的示例代码可以使用gcc -oExample Example.c进行编译,只需编写Example即可执行。但是要编译 myfunc.c 我首先需要编写 gcc -c myfunc.c -omyfunc.o 来创建一个目标文件,而不是通过使用 gcc -oPractice Practice.c myfunc.o.Than 与 Practice.c 链接来编译它程序被执行。但我正在尝试编译并执行一次 myfunc.c 并能够调用它的所有函数,就像编写 #include&lt;stdio.h&gt; 一样。
  • 没人做,不推荐,会导致各种问题。 C 库是一个大而复杂的东西。触摸它几乎肯定会破坏您机器上的东西。不要那样做。你这样做的方式是正常的和被接受的方式。

标签: c function header-files


【解决方案1】:

您可以根据以下条件制作自己的库函数文件: http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html Windows 的更多信息; Building a shared library using gcc on Linux and MinGW on Windows

【讨论】:

    【解决方案2】:

    经过几次搜索和尝试,我终于能够执行这个问题的答案。 我想和你分享。

                                       Answer 1
    

    myfunc.c 包含函数的定义(这里它们是power & prime)。根据定义,我的意思是,它包含带有实际代码的函数,这些函数将在运行时调用其中一个或两个函数时执行。

    myfunc.h包含这些函数的完整声明,每个函数声明的末尾都使用了;(semicolon)

    步骤:

    1 : 使用以下代码编译程序:gcc -c Practice.c myfunc.c。不需要同时编译两个文件(Practice.c & myfunc.c)。一个可以编译 分别:gcc -c Practice.c & gcc -c myfunc.c,后面的部分会比较费时间,既然可以一次完成,为什么还要跑两次呢。

    2 : 编译后你的当前目录包含两个额外的文件,即: Practice.o & myfunc.o。您将需要这两个文件。使用这个:gcc Practice.o myfunc.o -oPractice

    3 :2nd 步骤之后,写 Practice强> 在cmd。它将开始工作。


    注意:这里的重要部分是 myfunc.o。该文件可以与您的任何其他 C 程序一起使用,包含 main 函数。


    对于EXAMPLE,如果我们有以下程序使用myfunc.c文件中的函数,那么

    Test.c 包含 main()

    #include <stdio.h>
    #include "myfunc.h"  //double quotes here states that # will first look for the 
                         //header file in the current directory & later in the C library.
                         //And will replace this line during compilation with
                         //the declaration of functions present in this header file.
                         //This will inform the compiler that there exists a function of
                         //the particular type.
    
    
    main()
    {
      int i,k;
    
      printf("\nEnter Number = ");
      scanf("%d",&i);
    
      k=prime(i);          //During the execution of this line, runtime will need an *`object(.o)`*
                           //file with *`Test.c`*. Else your program won't work.
    
    
      printf("\n  IS %d PRIME = %c",i,k);
    }
    

    使用以下代码:gcc Test.c myfunc.o -oTest。您不需要每次都编译myfunc.c,而是一次。

    阅读以下问题的答案,正确编译源代码:LINK

                                          Answer 2
    

    关于预编译头文件的信息可以在Hottest 'pre-compiled header files' Answers找到

    【讨论】:

      【解决方案3】:

      在 myfunc.c 中添加#include "myfunc.h" 并尝试 gcc -c practice.c myfunc.c myfunc.h gcc -o practice practice.o myfunc.o

      【讨论】:

      • 我试过了,效果很好。但是,当我写这个 #include "myfunc.h" 时,究竟会发生什么?请详细说明。 @user3498984
      • 在 myfunc.c 中使用#include "myfunc.h" 是在告诉编译器,myfunc.h 的函数是在 myfunc.c 中定义的
      • 也就是说我每次编译都得写你刚才写的代码,才能执行程序?? @Badda
      猜你喜欢
      • 1970-01-01
      • 2018-04-04
      • 1970-01-01
      • 1970-01-01
      • 2017-07-27
      • 1970-01-01
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      相关资源
      最近更新 更多