如何在C ++ / Eclipse中创建包含的独立库

时间:2022-09-01 16:58:03

I've gotten some C++ code to work with the TinyXML parser. However, to do this I had to include the source code from TinyXML with my regular source code. I'd like to have TinyXML included as a separate library. I'm using Eclipse with the Cygwin C++ compiler. What's a good way to do this?

我已经获得了一些C ++代码来使用TinyXML解析器。但是,要做到这一点,我必须使用常规源代码包含TinyXML的源代码。我想将TinyXML作为一个单独的库包含在内。我正在使用Eclipse和Cygwin C ++编译器。这样做的好方法是什么?

2 个解决方案

#1


I assume you want to separate the library from your own project's source code... but you don't know how to build when the library is not in the same folder.

我假设您要将库与您自己项目的源代码分开...但是当库不在同一文件夹中时您不知道如何构建库。


Assuming your library has precompiled *.lib and *.h files:

假设您的库已经预编译了* .lib和* .h文件:

  1. Move the library source code to a separate directory
  2. 将库源代码移动到单独的目录中

  3. Menubar "project"
  4. Menu "properties" will open a dialog box for all the project properties there will be a list on the left.
  5. 菜单“属性”将打开所有项目属性的对话框,左侧将有一个列表。

  6. List item "C/C++ Build" will change the GUI and show you all the options for gcc's compiler/linker/assembler ( I never do assembly... so I never do anything with the assembler ). [1]
  7. 列表项“C / C ++ Build”将更改GUI并显示gcc编译器/链接器/汇编器的所有选项(我从不进行汇编...所以我从不对汇编器做任何事情)。 [1]

  8. GCC C Compiler --> Directories:
    如何在C ++ / Eclipse中创建包含的独立库
  9. GCC C编译器 - >目录:

  10. Green plus icon [2] --> Specify the path of your *.h files
  11. 绿色加图标[2] - >指定* .h文件的路径

  12. Your compiler should now be happy ( but you will fail linking because the linker doesn't know what the actual definitions of each function are )
  13. 您的编译器现在应该很高兴(但是您将无法链接,因为链接器不知道每个函数的实际定义是什么)

  14. GCC C Linker --> Libraries:
    如何在C ++ / Eclipse中创建包含的独立库
  15. GCC C链接器 - >库:

  16. Library search path (-L) --> Green plus icon --> Specify the path of your *.lib files
  17. 库搜索路径(-L) - >绿色加图标 - >指定* .lib文件的路径

  18. Libraries (-l) --> Green plus icon --> Specify the name of each library you are using
  19. 库(-l) - >绿色加图标 - >指定您正在使用的每个库的名称

  20. Your linker should now be happy and your code should compile
  21. 你的链接器现在应该很开心,你的代码应该编译

[Footnote - 1] The GUI C/C++ build pane is a wrapper for gcc's command line compiler/linker... it is just making it easier to use because it shows you everything visually.

[脚注 - 1] GUI C / C ++构建窗格是gcc命令行编译器/链接器的包装器......它只是使它更容易使用,因为它可以直观地显示所有内容。

[Footnote - 2] The '+' icon is what will tell the compiler where your libraries *.h include files are located. The compiler needs the *.h files to know what function prototypes your library has before it compiles.

[脚注 - 2]'+'图标将告诉编译器库* .h包含文件的位置。编译器需要* .h文件才能知道库编译之前的原型函数。


Assuming you have the actual ( not compiled ) *.c and *.h:

假设你有实际的(未编译的)* .c和* .h:

  1. Do the same steps above except in step 7.
  2. 除步骤7外,执行相同的步骤。

  3. At step 7. you need to make sure the library's *.c files are seen by Eclipse's "managed make". If it doesn't see the source code then you need to specify where the source is so that it will compile it.
  4. 在第7步,您需要确保Eclipse的“托管make”可以看到库的* .c文件。如果它没有看到源代码,那么你需要指定源的位置,以便编译它。

#2


It's basically easy. You compile your source code for the library, and construct the library with ar(1). Yes, surprise, a library is just an archive; UNIX is cool that way.

这基本上很容易。您编译库的源代码,并使用ar(1)构造库。是的,出乎意料的是,图书馆只是一个档案馆; UNIX很酷。

You can then include the code as a static library when you build the final code.

然后,您可以在构建最终代码时将代码包含为静态库。

I don't use Eclipse all that much so I can't tell you the exact process within the IDE, but I believe what you need is to set up a separate project to build it.

我没有那么多使用Eclipse,所以我无法告诉你IDE中的确切过程,但我相信你需要的是建立一个单独的项目来构建它。

Now, if what you want is to build a DLL, then you need to use some special flags. There's a nice page here.

现在,如果你想要的是构建一个DLL,那么你需要使用一些特殊的标志。这里有一个很好的页面。

#1


I assume you want to separate the library from your own project's source code... but you don't know how to build when the library is not in the same folder.

我假设您要将库与您自己项目的源代码分开...但是当库不在同一文件夹中时您不知道如何构建库。


Assuming your library has precompiled *.lib and *.h files:

假设您的库已经预编译了* .lib和* .h文件:

  1. Move the library source code to a separate directory
  2. 将库源代码移动到单独的目录中

  3. Menubar "project"
  4. Menu "properties" will open a dialog box for all the project properties there will be a list on the left.
  5. 菜单“属性”将打开所有项目属性的对话框,左侧将有一个列表。

  6. List item "C/C++ Build" will change the GUI and show you all the options for gcc's compiler/linker/assembler ( I never do assembly... so I never do anything with the assembler ). [1]
  7. 列表项“C / C ++ Build”将更改GUI并显示gcc编译器/链接器/汇编器的所有选项(我从不进行汇编...所以我从不对汇编器做任何事情)。 [1]

  8. GCC C Compiler --> Directories:
    如何在C ++ / Eclipse中创建包含的独立库
  9. GCC C编译器 - >目录:

  10. Green plus icon [2] --> Specify the path of your *.h files
  11. 绿色加图标[2] - >指定* .h文件的路径

  12. Your compiler should now be happy ( but you will fail linking because the linker doesn't know what the actual definitions of each function are )
  13. 您的编译器现在应该很高兴(但是您将无法链接,因为链接器不知道每个函数的实际定义是什么)

  14. GCC C Linker --> Libraries:
    如何在C ++ / Eclipse中创建包含的独立库
  15. GCC C链接器 - >库:

  16. Library search path (-L) --> Green plus icon --> Specify the path of your *.lib files
  17. 库搜索路径(-L) - >绿色加图标 - >指定* .lib文件的路径

  18. Libraries (-l) --> Green plus icon --> Specify the name of each library you are using
  19. 库(-l) - >绿色加图标 - >指定您正在使用的每个库的名称

  20. Your linker should now be happy and your code should compile
  21. 你的链接器现在应该很开心,你的代码应该编译

[Footnote - 1] The GUI C/C++ build pane is a wrapper for gcc's command line compiler/linker... it is just making it easier to use because it shows you everything visually.

[脚注 - 1] GUI C / C ++构建窗格是gcc命令行编译器/链接器的包装器......它只是使它更容易使用,因为它可以直观地显示所有内容。

[Footnote - 2] The '+' icon is what will tell the compiler where your libraries *.h include files are located. The compiler needs the *.h files to know what function prototypes your library has before it compiles.

[脚注 - 2]'+'图标将告诉编译器库* .h包含文件的位置。编译器需要* .h文件才能知道库编译之前的原型函数。


Assuming you have the actual ( not compiled ) *.c and *.h:

假设你有实际的(未编译的)* .c和* .h:

  1. Do the same steps above except in step 7.
  2. 除步骤7外,执行相同的步骤。

  3. At step 7. you need to make sure the library's *.c files are seen by Eclipse's "managed make". If it doesn't see the source code then you need to specify where the source is so that it will compile it.
  4. 在第7步,您需要确保Eclipse的“托管make”可以看到库的* .c文件。如果它没有看到源代码,那么你需要指定源的位置,以便编译它。

#2


It's basically easy. You compile your source code for the library, and construct the library with ar(1). Yes, surprise, a library is just an archive; UNIX is cool that way.

这基本上很容易。您编译库的源代码,并使用ar(1)构造库。是的,出乎意料的是,图书馆只是一个档案馆; UNIX很酷。

You can then include the code as a static library when you build the final code.

然后,您可以在构建最终代码时将代码包含为静态库。

I don't use Eclipse all that much so I can't tell you the exact process within the IDE, but I believe what you need is to set up a separate project to build it.

我没有那么多使用Eclipse,所以我无法告诉你IDE中的确切过程,但我相信你需要的是建立一个单独的项目来构建它。

Now, if what you want is to build a DLL, then you need to use some special flags. There's a nice page here.

现在,如果你想要的是构建一个DLL,那么你需要使用一些特殊的标志。这里有一个很好的页面。