如何在像cl这样的c ++中创建一个库

时间:2021-08-25 13:16:49

I have made my own implementations of many of the STL features like Vectors, Lists, BST, Queue, Stack and given them all the functions that an STL corresponding library has....

我已经自己实现了许多STL功能,如矢量,列表,BST,队列,堆栈,并为它们提供了STL相应库所具有的所有功能....

Now i want to use this library by

现在我想使用这个库

#include "myLibName.h"

What I Did :

我做了什么 :

g++ -o -c myLib myLib.cpp

From This I got the object file...

从这里我得到了目标文件......

But when i compile programs i have to link the object file myself...
Is there any way that i can do without linking...like the iostream and the other libraries are linked automatically.

但是当我编译程序时,我必须自己链接目标文件...有没有办法,我可以做到没有链接...像iostream和其他库自动链接。

I know that a SHARED OBJECT file (eg. libc.so in C) is where all the implementations are held in C.... If that's the solution then how do i make any and use it like other standard libraries in C++ without linking object file every time.

我知道一个共享对象文件(例如C中的libc.so)是所有实现都保存在C中的地方....如果这是解决方案那么我如何制作任何并使用它像C ++中的其他标准库而不链接每次都有目标文件。

PS: After a lot of efforts i have created these libraries myself...Now Struck at the final step...Pls Help...

PS:经过我的努力,我自己创建了这些库...现在最后一步......请帮助......

3 个解决方案

#1


1  

To make your build process cleaner for large projects in which you need to link multiple projects, you can use Makefiles.
After that you need to just type make at the terminal to compile and build the whole project.

要使您需要链接多个项目的大型项目的构建过程更清晰,可以使用Makefile。之后,您需要在终端输入make来编译和构建整个项目。

Another solution is the following, although many people don't recommend it,

另一种解决方案如下,虽然很多人不推荐它,

      header.h
     class Foo
     { 
         // some variable and method declarations. 
     }

header.h is your header file which will contain your declarations.

header.h是您的头文件,其中包含您的声明。

    implement.cpp // this is the implementation file
    #include "header.h" 
    // Now implement various methods you declared in your "header.h" file. 

implement.cpp is your implementation file which contains the implementation and the definition of static members.

implement.cpp是您的实现文件,其中包含静态成员的实现和定义。

    main.cpp
    #include "header.cpp" 
    // use your methods. 

Now you don't need to link your object files, just do g++ -Wall main.cpp

现在你不需要链接你的目标文件,只需要做g ++ -Wall main.cpp

#2


4  

You can't unless you're going to write your own toolchain. GCC links in its runtime and standard library because it's GCC and knows that it should; it won't magically do the same with your library.

除非你要编写自己的工具链,否则你不能。 GCC在其运行时和标准库中链接,因为它是GCC并且知道它应该;它不会神奇地对你的图书馆做同样的事情。

Conventionally, either make your library header-only or ship a .a/.so/.dll for devs to link against at linktime. In the latter two cases you'll also need to ship the .so/.dll for users to link against at runtime.

通常,要么使您的库只有标头,要么运送.a / .so / .dll以便开发者在链接时链接。在后两种情况下,您还需要运送.so / .dll以供用户在运行时链接。

#3


1  

First of all, you should probably differentiate between STL and the Standard C++ Library.

首先,您应该区分STL和标准C ++库。

Each compiler comes with its own implementation of the Standard Library, some of them being (at least mostly) compatible (see clang++ and g++). So basically your way to go would be to modify the compiler you are using.

每个编译器都有自己的标准库实现,其中一些(至少大部分)兼容(参见clang ++和g ++)。所以基本上你的方法是修改你正在使用的编译器。

If you are writing header-only implementations, then no library is needed to be built and you can use it without linking. But in that case your work has to be distributed as source and not as library + header.

如果您正在编写仅限标头的实现,则不需要构建任何库,您可以在不链接的情况下使用它。但在这种情况下,您的工作必须作为源而不是库+标头分发。

If you want to simply distribute your library and do not mind to link against the shared or static library you distributed, you should build a shared or static library, depending on the case. But you will have to link it when it is used.

如果您只想简单地分发库并且不介意链接到您分发的共享库或静态库,则应根据具体情况构建共享库或静态库。但是在使用它时你必须链接它。

#1


1  

To make your build process cleaner for large projects in which you need to link multiple projects, you can use Makefiles.
After that you need to just type make at the terminal to compile and build the whole project.

要使您需要链接多个项目的大型项目的构建过程更清晰,可以使用Makefile。之后,您需要在终端输入make来编译和构建整个项目。

Another solution is the following, although many people don't recommend it,

另一种解决方案如下,虽然很多人不推荐它,

      header.h
     class Foo
     { 
         // some variable and method declarations. 
     }

header.h is your header file which will contain your declarations.

header.h是您的头文件,其中包含您的声明。

    implement.cpp // this is the implementation file
    #include "header.h" 
    // Now implement various methods you declared in your "header.h" file. 

implement.cpp is your implementation file which contains the implementation and the definition of static members.

implement.cpp是您的实现文件,其中包含静态成员的实现和定义。

    main.cpp
    #include "header.cpp" 
    // use your methods. 

Now you don't need to link your object files, just do g++ -Wall main.cpp

现在你不需要链接你的目标文件,只需要做g ++ -Wall main.cpp

#2


4  

You can't unless you're going to write your own toolchain. GCC links in its runtime and standard library because it's GCC and knows that it should; it won't magically do the same with your library.

除非你要编写自己的工具链,否则你不能。 GCC在其运行时和标准库中链接,因为它是GCC并且知道它应该;它不会神奇地对你的图书馆做同样的事情。

Conventionally, either make your library header-only or ship a .a/.so/.dll for devs to link against at linktime. In the latter two cases you'll also need to ship the .so/.dll for users to link against at runtime.

通常,要么使您的库只有标头,要么运送.a / .so / .dll以便开发者在链接时链接。在后两种情况下,您还需要运送.so / .dll以供用户在运行时链接。

#3


1  

First of all, you should probably differentiate between STL and the Standard C++ Library.

首先,您应该区分STL和标准C ++库。

Each compiler comes with its own implementation of the Standard Library, some of them being (at least mostly) compatible (see clang++ and g++). So basically your way to go would be to modify the compiler you are using.

每个编译器都有自己的标准库实现,其中一些(至少大部分)兼容(参见clang ++和g ++)。所以基本上你的方法是修改你正在使用的编译器。

If you are writing header-only implementations, then no library is needed to be built and you can use it without linking. But in that case your work has to be distributed as source and not as library + header.

如果您正在编写仅限标头的实现,则不需要构建任何库,您可以在不链接的情况下使用它。但在这种情况下,您的工作必须作为源而不是库+标头分发。

If you want to simply distribute your library and do not mind to link against the shared or static library you distributed, you should build a shared or static library, depending on the case. But you will have to link it when it is used.

如果您只想简单地分发库并且不介意链接到您分发的共享库或静态库,则应根据具体情况构建共享库或静态库。但是在使用它时你必须链接它。