自定义 C++ 静态库未链接 (Code::Blocks)
Custom C++ Static Library Not Linking (Code::Blocks)
过去两个小时我一直在网上研究这个问题,但无济于事 - 我已经多次按照所有说明进行操作。
我正在使用 C++11、GCC、Ubuntu 15.04 和 Code::Blocks 13.12。
我正在创建自定义静态库。目前,它只包含两个文件:iochannel.hpp
和 iochannel.cpp
。我编译,一切都很好。在 pawlib/bin/Debug
中生成了单个 libpawlib.a
文件
接下来,在我要使用静态库的项目中,我进入Project > Build Options...
。我将 .a 文件的路径添加到 Linker settings
,并将找到 .a 文件的文件夹添加到 Search Directories > Compiler
和 > Linker
我已经检查过,double-checked, tripe-checked 和 quadruple-checked 路径。
只要我不尝试从库中导入,该项目的构建就会成功,并且会运行。但是,无论我如何放置 #include
语句,在我编译时它都会显示 "No such file or directory" 。我已经尝试了以下所有(单独)...
#include <iochannel>
#include <iochannel.hpp>
#include <pawlib/iochannel>
#include <pawlib/iochannel.hpp>
#include "iochannel"
#include "iochannel.hpp"
#include "pawlib/iochannel"
#include "pawlib/iochannel.hpp"
据我所知,网上没有比我已经遵循的更多的说明了。
这在理论上是一件微不足道的事情,但我很难过。谁能帮我解决这个问题?
编辑: 我的文件结构如下。在一个文件夹(实际上,我的项目存储库的根目录)中,我有两个文件夹:pawlib
和 pawlib-test
。它包含以下重要文件...
pawlib/bin/Debug/libpawlib.a
pawlib/src/iochannel.cpp
pawlib/include/iochannel.hpp
pawlib-test
中的项目正在使用相对路径 ../pawlib/bin/Debug/libpawlib.a
或 ../pawlib/bin/Debug
,具体取决于它需要文件路径还是目录路径。绝对路径也失败了。我已确认文件和文件夹存在于这些路径中。
重要提示: 我不必手动包含 pawlib 的 .hpp 文件,因为这违背了库的全部目的。我经常使用静态库,而且我从不 必须将 .hpp 文件或其目录添加到搜索目录中。
gcc 命令及其错误(由 CodeBlocks 生成)如下...
-------------- Build: Debug in pawlib-test (compiler: GNU GCC Compiler)---------------
g++ -std=c++11 -std=c++11 -Wall -g -std=c++11 -g -I../pawlib/bin/Debug -c /home/jason/Code/Repositories/pawlib-git/pawlib-test/main.cpp -o obj/Debug/main.o
/home/jason/Code/Repositories/pawlib-git/pawlib-test/main.cpp:2:31: fatal error: pawlib/iostream.hpp: No such file or directory
#include "pawlib/iostream.hpp"
^
compilation terminated.
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))
编辑 2: 我的错误(部分)...headers 通常与静态库分开导入。问题是,几乎 none 的教程或文档都提到了这一点。这是 "everyone knows, so it should go without saying" 类型的事情之一。
我对静态库和动态库感到困惑。当您 link 访问静态库时,您还必须 link 访问其中的 header 文件。这就是为什么您在任何 UNIX 机器上都能找到 /usr/lib
(对于静态库)和 /usr/include
(对于它们的 headers)文件夹。
正如 francis 所指出的,这与我没有提供 header 文件的路径有关。如果您使用的是 CodeBlocks,请转到 Build Options-> Search directories -> Compiler
对话框,然后将路径添加到包含 .h
或 .hpp
文件的文件夹。
现在效果很好。
过去两个小时我一直在网上研究这个问题,但无济于事 - 我已经多次按照所有说明进行操作。
我正在使用 C++11、GCC、Ubuntu 15.04 和 Code::Blocks 13.12。
我正在创建自定义静态库。目前,它只包含两个文件:iochannel.hpp
和 iochannel.cpp
。我编译,一切都很好。在 pawlib/bin/Debug
libpawlib.a
文件
接下来,在我要使用静态库的项目中,我进入Project > Build Options...
。我将 .a 文件的路径添加到 Linker settings
,并将找到 .a 文件的文件夹添加到 Search Directories > Compiler
和 > Linker
我已经检查过,double-checked, tripe-checked 和 quadruple-checked 路径。
只要我不尝试从库中导入,该项目的构建就会成功,并且会运行。但是,无论我如何放置 #include
语句,在我编译时它都会显示 "No such file or directory" 。我已经尝试了以下所有(单独)...
#include <iochannel>
#include <iochannel.hpp>
#include <pawlib/iochannel>
#include <pawlib/iochannel.hpp>
#include "iochannel"
#include "iochannel.hpp"
#include "pawlib/iochannel"
#include "pawlib/iochannel.hpp"
据我所知,网上没有比我已经遵循的更多的说明了。
这在理论上是一件微不足道的事情,但我很难过。谁能帮我解决这个问题?
编辑: 我的文件结构如下。在一个文件夹(实际上,我的项目存储库的根目录)中,我有两个文件夹:pawlib
和 pawlib-test
。它包含以下重要文件...
pawlib/bin/Debug/libpawlib.a
pawlib/src/iochannel.cpp
pawlib/include/iochannel.hpp
pawlib-test
中的项目正在使用相对路径 ../pawlib/bin/Debug/libpawlib.a
或 ../pawlib/bin/Debug
,具体取决于它需要文件路径还是目录路径。绝对路径也失败了。我已确认文件和文件夹存在于这些路径中。
重要提示: 我不必手动包含 pawlib 的 .hpp 文件,因为这违背了库的全部目的。我经常使用静态库,而且我从不 必须将 .hpp 文件或其目录添加到搜索目录中。
gcc 命令及其错误(由 CodeBlocks 生成)如下...
-------------- Build: Debug in pawlib-test (compiler: GNU GCC Compiler)---------------
g++ -std=c++11 -std=c++11 -Wall -g -std=c++11 -g -I../pawlib/bin/Debug -c /home/jason/Code/Repositories/pawlib-git/pawlib-test/main.cpp -o obj/Debug/main.o
/home/jason/Code/Repositories/pawlib-git/pawlib-test/main.cpp:2:31: fatal error: pawlib/iostream.hpp: No such file or directory
#include "pawlib/iostream.hpp"
^
compilation terminated.
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))
编辑 2: 我的错误(部分)...headers 通常与静态库分开导入。问题是,几乎 none 的教程或文档都提到了这一点。这是 "everyone knows, so it should go without saying" 类型的事情之一。
我对静态库和动态库感到困惑。当您 link 访问静态库时,您还必须 link 访问其中的 header 文件。这就是为什么您在任何 UNIX 机器上都能找到 /usr/lib
(对于静态库)和 /usr/include
(对于它们的 headers)文件夹。
正如 francis 所指出的,这与我没有提供 header 文件的路径有关。如果您使用的是 CodeBlocks,请转到 Build Options-> Search directories -> Compiler
对话框,然后将路径添加到包含 .h
或 .hpp
文件的文件夹。
现在效果很好。