简体   繁体   English

如何用C中的通用代码实现可执行文件和共享库

[英]How to implement executable and shared library with common code in C

I'm designing a Linux C project which is composed by two binaries: an executable and a shared library. 我正在设计一个由两个二进制文件组成的Linux C项目:可执行文件和共享库。

I have the following project structure using Eclipse-cdt: 我使用Eclipse-cdt具有以下项目结构:

Main Project Executable
    +-----folderA
        ------------- sourceA.c
        ------------- sourceA.h
    +-----folderB
        ------------- sourceB.c
        ------------- sourceB.h
    main.c

Shared Library Project
    +-----folderB
        ------------- libcode.c
        ------------- libcode.h

The Shared Library code in libcode.c is strictly dependent on folderA and folderB code from the Main Project. libcode.c中的共享库代码严格依赖于主项目中的folderAfolderB代码。

How could I organize the projects in order to have no duplicated files? 如何组织项目以确保没有重复的文件?

Solution A (Current) 解决方案A(当前)

The best solution I've found so far, is to use linked resources inside eclipse: 到目前为止,我发现的最佳解决方案是在eclipse中使用链接资源:

Main Project Executable
    +-----folderA
        ------------- sourceA.c
        ------------- sourceA.h
    +-----folderB
        ------------- sourceB.c
        ------------- sourceB.h
        ------------- libcode.c
        ------------- libcode.h
    main.c

Shared Library Project
    +-----folderA [linked]
        ------------- sourceA.c [linked]
        ------------- sourceA.h [linked]
    +-----folderB [linked]
        ------------- sourceB.c [linked]
        ------------- sourceB.h [linked]
        ------------- libcode.c [linked]
        ------------- libcode.h [linked]

Notice that I moved 100% of shared library code into the main project and linked all the files I needed in the Shared Library. 请注意,我将100%的共享库代码移动到主项目中,并链接了共享库中所需的所有文件。 This way I don't need to have any "physical" source files inside the Shared Library Project, but I still think it's not a good idea, because it can surprise other developers (violating POLS principle). 这样,我在共享库项目中不需要任何“物理”源文件,但是我仍然认为这不是一个好主意,因为它会使其他开发人员感到惊讶(违反POLS原理)。

Solution B 解决方案B.

There is the option to move all the common code into a third project and make the two projects depend on it, but in my case it will lead into a big code refactoring, which I would like to avoid. 可以选择将所有通用代码移入第三个项目,并使这两个项目都依赖于此,但就我而言,这将导致大量代码重构,我想避免这种情况。

Any other possibility? 还有其他可能吗? Is the solution B definetly a better practice? 解决方案B肯定是一种更好的做法吗?

You should probably put the source in folderA and folderB into your shared library since they apparently contain core functionality. 您可能应该将源文件中的folderAfolderB放入共享库中,因为它们显然包含核心功能。 In your executable put the source that uses the library to do something, for example, calling various functions in libcode to achieve some goal for the end user. 在您的可执行文件中,使用库来执行某些操作,例如,在libcode调用各种函数以实现最终用户的某些目标。 You will need to link the executable to the shared library. 您需要将可执行文件链接到共享库。

Main Project Executable [links to shared library] 
    main.c              [uses libcode to do stuff]

Shared Library Project
    +-----folderA
        ------------- sourceA.c
        ------------- sourceA.h
    +-----folderB
        ------------- sourceB.c
        ------------- sourceB.h
        ------------- libcode.c
        ------------- libcode.h

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

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