zoukankan      html  css  js  c++  java
  • C语言创建并使用lib

    本文试图以比较简洁的方式创建lib:

    只求能够把lib用起来,并不会加上【很多但必须的东西,比如我们之前说过的#ifndef #define 和#endif】

    打开vs

    创建一个新的项目:

    点击确定

    然后添加 -> 新建项

    这个名称很关键: main.c

    Main.c的内容:

     

     

    int myMultiple(int a, int b){
        return a*b;
    }

     

    /*

    这是因为如果使用,main.cpp的话,这个文件就会以cpp的形式编译,这样的话,还要有另外的语法extern格式来限定。所以我们在创建的时候倒不如直接写main.c。这样省一步事儿。

    C语言可以操作很多东西,很多人用它来写操作系统,这是因为 C语言可以直接操作硬件,对于很多上层语言来说,他们理论上不具有 这样的能力,或者即使有,也很复杂,写起来很麻烦,所以 C语言在一定程度上用来专门书写这种静态库文件lib和 动态库文件dll

    所以这里想说的是就写main.c 就可以啦!!!

    非要写main.cpp的话,要这样包一层:

        extern "C" //在C++语言当中,extern "C"告诉编译器,用C语言的方式编译这个函数
        {
          int myMultiple(int a, int b){
              return a*b;
          }
        }

    */

    然后去 项目->属性->配置属性->配饰类型   修改成 静态库(.lib.)

    然后新建一个项目showLib_Use,来使用这个 lib

    添加-> 新建项 

    这个时候 注意 先用main.c 创建

     

    然后生成

    Main.c 的内容:

    #include <stdio.h>
    #pragma comment(lib,"showLib.lib")
    int myMultiple(int, int);
    int main(){
        printf("lifei
    ");
        printf("%d", myMultiple(2, 3));
        getchar();
        return 0;
    }

    在此之前需要先把 showLib.lib放到静态时候要在编译时候使用它的文件目录下面。

     

    看了我的几个例程发现我都只是用.c文件实现了 lib的加载 然后 用 .cpp 文件和.c实现了 dll的加载。

     

    //苦恼中。。。求高玩帮忙解答下

     

  • 相关阅读:
    LeetCode 109 Convert Sorted List to Binary Search Tree
    LeetCode 108 Convert Sorted Array to Binary Search Tree
    LeetCode 107. Binary Tree Level Order Traversal II
    LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
    LeetCode 103 Binary Tree Zigzag Level Order Traversal
    LeetCode 102. Binary Tree Level Order Traversal
    LeetCode 104. Maximum Depth of Binary Tree
    接口和多态性
    C# 编码规范
  • 原文地址:https://www.cnblogs.com/letben/p/5223264.html
Copyright © 2011-2022 走看看