简体   繁体   English

如何将类的对象文件与源文件链接

[英]How to link an Object file of class with a source file

I want to know if i can link and compile source code file with an object file of a class without including class source files. 我想知道是否可以在不包括类源文件的情况下将源代码文件与类的目标文件链接和编译。

Or 要么

Like in Java i can give a compiled *.class file to a friend and the doc of that class without giving the source code and he can include that class for his need without knowing the real source. 像在Java中一样,我可以在不提供源代码的情况下将编译后的* .class文件提供给朋友和该类的文档,并且他可以根据需要添加该类而无需知道真实的源代码。

Can i do the exact thing with C++ ? 我可以用C ++做确切的事情吗? and How ? 如何 ?

FYI:I'm using MinGW. 仅供参考:我正在使用MinGW。

I'm quite new to so this may look like a dumb question (but still there's no dumb questions). 我是新手,所以这看起来像是一个愚蠢的问题(但仍然没有愚蠢的问题)。

In C++ you can just produce an executable file (.exe in windows, usually no extension in Linux) or a library file (.lib or .a) or a shared libarary (.dll or .so) depending on what you want to achieve. 在C ++中,您只可以生成一个可执行文件(在Windows中为.exe,在Linux中通常没有扩展名)或一个库文件(.lib或.a)或一个共享库(.dll或.so),具体取决于您要实现的目标。 。

If you are providing a library, you will have to provide a header file with the class declarations as well. 如果要提供库,则还必须提供带有类声明的头文件。 If you don't want to give out too much about the details, you can have a "implementation" class that is simply a pointer to the implementation in your header file, and the real implementation is only available to you as the owner of the sources. 如果您不想透露太多细节,则可以有一个“实现”类,该类只是指向头文件中的实现的指针,而实际的实现仅作为您的所有者提供给您。资料来源。

For more information, you should look up "PIMPL" as Thomas Matthews suggested - that's short for "Pointer to IMPLementation class". 有关更多信息,您应该按照Thomas Matthews的建议查找“ PIMPL”,这是“指向IMPLementation类的指针”的缩写。 Basically, your "public" class is just a shell providing the functions you want others to see, and the implementation class inside it does all the hard work. 基本上,您的“公共”类只是一个外壳,提供您希望其他人看到的功能,而其中的实现类则完成了所有艰苦的工作。

Simple example 简单的例子

In your header file: 在头文件中:

// Forward declaration of the implementation class. 
class double_it_impl;

// Class that stores an integer, and doubles it each time you 
// call doubling().
class Public_double_it
{
  public:
    public_double_it(int x);    // COnstructor. 

    int doubling();        // Function d

  private:
     double_it_impl *pImpl;
};

In your source or private header file, we declare the actual implementation: 在您的源或私有头文件中,我们声明实际的实现:

class double_it_impl
{
  public:
    double_it_impl(int x) : m_x = x; {};
    int doubling()  { m_x *= 2; return m_x; }

}


public_double_it::public_double_it(int x)
{
    pImpl = new public_double_it(x);
}

int public_double_it::doubling()
{
   return pImpl->doubling();
}

Now you can't see what the implementation class contains or how it works - of course, this is a very trivial example, but I hope it conveys the idea. 现在您看不到实现类包含的内容或它的工作方式-当然,这是一个非常简单的示例,但是我希望它能传达想法。

You can give them the *.class but they can be decompiled. 您可以给他们* .class,但可以将它们反编译。

Most libraries are provide using the classes and the generated Javadoc. 大多数库是使用类和生成的Javadoc提供的。

You could compile the class into a library or dll or shared library and give to your friend. 您可以将类编译为一个库或dll或共享库,然后提供给您的朋友。

They would need a header file that defines the interface. 他们将需要一个定义接口的头文件。

See also PIMPL idiom. 另请参阅PIMPL惯用语。

Yes, you can provide a compiled object file and documentation and others can link the .o (or .obj) file into their code. 是的,您可以提供已编译的目标文件和文档,其他人可以将.o(或.obj)文件链接到其代码中。 The exact details of how to do this vary between compilers and development environments. 如何执行此操作的确切详细信息在编译器和开发环境之间有所不同。 If you are using g++, you simply use the following command-line: 如果您使用的是g ++,则只需使用以下命令行:

g++ -c source.cpp

This will create a .o file which you (or anyone else) can then link with other .o files with 这将创建一个.o文件,您(或其他任何人)可以将其与其他.o文件链接

g++ main.o source.o

Other compilers will have similar options. 其他编译器将具有类似的选项。 Read the help files and other documentation for instructions about how to create object files. 阅读帮助文件和其他文档,以获取有关如何创建目标文件的说明。

For a large number of functions and/or classes, you should look at creating a library file. 对于大量的函数和/或类,您应该考虑创建一个库文件。 These can be statically or dynamically linked. 这些可以静态或动态链接。 I suggest that you search the Web for more information if you want to learn about how to create your own library files. 如果您想了解如何创建自己的库文件,建议您在Web上搜索更多信息。

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

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