首页 > 其他分享 >使用GNU编译器套件链接库

使用GNU编译器套件链接库

时间:2023-09-17 16:57:13浏览次数:41  
标签:prime gcc libm GNU int 链接库 编译器 xuanmiao interest

1、当前目录 /home/xuanmiao/Demo/LSPT/Test 创建文件prime.h和prime.c

prime.h

int isprime(long int number);

prime.c

int isprime(long int number)
{
   long int j;
   int prime = 1;
    
   /* Test if the number is divisible, starting 
    * from 2 */
   for(j=2; j<number; j++)
   {
      /* Use the modulo operator to test if the 
       * number is evenly divisible, i.e., a 
       * prime number */
      if(number%j == 0)
      {
         prime = 0;
      }
   }
   if(prime == 1)
   {
      return 1;
   }
   else
   {
      return 0;
   }
}

2、当前目录 /home/xuanmiao/Demo/LSPT/Test 根据prime.c编译生成目标文件prime.o

gcc  -Wall  -Wextra -pedantic -fPIC -c prime.c

-Wall:这是gcc编译器的一个选项,它告诉编译器输出所有可能的警告信息。

-Wextra:这个选项使gcc产生一些额外的警告,这些警告超出了-Wall选项产生的警告。

-pedantic:这个选项告诉gcc,要遵守所有的ISO C和ISO C++标准。

-fPIC:这个选项生成位置无关代码(Position Independent Code),这样的代码可以在任何地方执行,而不会受到自身所在内存位置的影响。\n- -c:这个选项告诉gcc编译或汇编源文件,但不要链接。

prime.c:这是你要编译的C源文件。

3、当前目录 /home/xuanmiao/Demo/LSPT/Test 根据目标文件prime.o生成动态链接库libprime.so

gcc -shared -Wl, soname, libprime.so -o libprime.so prime.o 

4、当前目录 /home/xuanmiao/Demo/LSPT/Test 编辑调用函数

is-it-a-prime.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "prime.h"

int main(int argc, char *argv[])
{
   long int num;
   /* Only one argument is accepted */
   if (argc != 2)
   {
      fprintf(stderr, "Usage: %s number\n", 
         argv[0]);
      return 1;
   }
   /* Only numbers 0-9 are accepted */
   if ( strspn(argv[1], "0123456789") != 
      strlen(argv[1]) )
   {
      fprintf(stderr, "Only numeric values are "
         "accepted\n");
      return 1;
   }
   num = atol(argv[1]); /* String to long */
   if (isprime(num)) /* Check if num is a prime */
   {
      printf("%ld is a prime\n", num);
   }
   else
   {
      printf("%ld is not a prime\n", num);
   }
    
   return 0;
}

5、当前目录 /home/xuanmiao/Demo/LSPT/Test 编译is-it-a-prime.c 源文件

gcc -L${PWD} is-it-a-prime.c -o is-it-a-prime -lprime

这条命令是在使用GCC编译器编译并链接一个C语言源文件(is-it-a-prime.c),同时链接到一个名为libprime的库。这里的各个参数的含义如下:\n- -L${PWD}:这个选项告诉gcc在哪里查找库文件,PWD表示当前目录。\n- is-it-a-prime.c:这是你要编译的源文件。\n- -o is-it-a-prime:这个选项指定了输出文件的名字。\n- -lprime:这个选项告诉gcc链接到名为libprime的库。

6、设置环境变量$LD_LIBRARY_PATH为当前目录

export LD_LIBRARY_PATH=PWD:{PWD}:PWD:{LD_LIBRARY_PATH}

7、例如下面文件interest.c中使用了pow函数,这个函数来自标准库中的math库libm,在ubuntu中在/usr/lib/x86_64-linux-gnu目录下

interest.c

#include <stdio.h>
#include <math.h>

int main(void)
{
    int years = 15; /* The number of years you will 
                     * keep the money in the bank 
                     * account */
    int savings = 99000; /* The inital amount */
    float interest = 1.5; /* The interest in % */

    printf("The total savings after %d years " 
        "is %.2f\n", years, 
        savings * pow(1+(interest/100), years));
    return 0;
}
xuanmiao@linux:/usr/lib/x86_64-linux-gnu$ pwd
/usr/lib/x86_64-linux-gnu
xuanmiao@linux:/usr/lib/x86_64-linux-gnu$ ls | grep [libm.so](http://libm.so)
[libm.so](http://libm.so)
libm.so.6

在编译interest.c时需要指定libm库:gcc interest.c -o interest -lm

标签:prime,gcc,libm,GNU,int,链接库,编译器,xuanmiao,interest
From: https://www.cnblogs.com/xuanmiao363/p/17709125.html

相关文章

  • Replacing gcc and g++ with GNU version in macOS
    AfterweinstallXcodeCommandLineTools,wewillgetgccandg++in/Library/Developer/CommandLineTools/usr/binandthesamecontentsin/usr/bin.Buttheproblemisthatgccandg++aresameasclangandclang++.Proofcanbeobtainedfromthefollowin......
  • Vrabche-一种Sysy语言编译器
    Vrabche-一种不是很完善的Sysy语言编译器g**l**©2023版权所有https://github.com/GammaMilk/Vrabche简介大赛要求各参赛队综合运用各种知识(包括但不局限于编译技术、操作系统、计算机体系结构等),构思并实现一个综合性的编译系统,以展示面向特定目标平台的编译器构造与编......
  • iTOP-RK3568开发板内核模块实验-设置交叉编译器
    在上一章节我们编写了最简单的helloworld驱动程序。有了驱动程序以后,要如何编译并使用驱动呢。编译驱动有俩种方法,分别是将驱动编译成内核和将驱动编译成内核模块。我们先来学习如何将驱动编译成内核模块、4.1设置交叉编译器1下载网盘资料下的交叉编译器,网盘路径为:“XXX”,将下......
  • mac上的find与gnu find
    linux上用的find属于gnufind,如果是查找当前路径是可以省略.的,只需要find-name"xxx"而mac上自带的find是bsdfind,.不可以省略,否则会报错/usr/bin/find:illegaloption--n可以手动安装https://www.gnu.org/software/findutilsbrewinstallfindutilsIfyouneedtouse......
  • GCC编译器
    (1)预处理(preprocessing)、编译(compilation)、汇编(assembly)和链接(linking) gcc-E-ohello.ihello.c //预处理(preprocessing) gcc-S-ohello.shello.i //编译(compilation) gcc-c-ohello.ohello.s //汇编(assembly) gcc-ohellohello.o //链接(link......
  • css预编译器: center;}
    CSS预编译器是一种用于构建CSS的工具,它可以将CSS代码转换为更易于管理和维护的格式。它们可以使CSS代码更加灵活,更易于重用,并且可以帮助开发人员更轻松地组织和管理CSS代码。CSS预编译器是一种用于构建CSS的工具,它可以将CSS代码转换为更易于管理和维护的格式。它们可以使CSS代码更......
  • Docker|--E: gnupg, gnupg2 and gnupg1 do not seem to be installed, but one of the
    错误apt-keyadv--keyserverkeyserver.ubuntu.com--recv-keys871920D1991BC93CE:gnupg,gnupg2andgnupg1donotseemtobeinstalled,butoneofthemisrequiredforthisoperation解决方案apt-getupdate&&apt-getinstall-ygnupg2#或者apt-getu......
  • GNU开发环境基础
    ---title:GNU开发环境基础date:2023-05-0110:22:27tags:---使用静态链接库(.a文件)1)首先使用gcc命令把源文件编译为目标文件,也即.o文件:gcc-c源文件列表2)然后使用ar命令将.o文件打包成静态链接库,具体格式为:  arrcs+静态库文件的名字+目标文件列表 ......
  • Django-CKEditor富文本编译器相关知识介绍
    安装pipinstalldjango-ckeditorpipinstallpillow注册富文本编译器在settings中的INSTALLED_APPS代码块中加入ckeditor和ckeditor_uploader(可支持图片上传)配置富文本编译器在settings中增加以下代码:#指定富文本编辑器或其他上传文件的根目录,这里为/test_blog/media......
  • 【开源三方库】bignumber.js:一个大数数学库
    OpenHarmony(OpenAtom OpenHarmony简称“OpenHarmony”)三方库,是经过验证可在OpenHarmony系统上可重复使用的软件组件,可帮助开发者快速开发OpenHarmony应用。如果是发布到开源社区,称为开源三方库,开发者可以通过访问开源社区获取。接下来我们来了解bignumber.js开源三方库。bignum......