Reputation: 6898
I've spent the past two hours researching this online, to no avail - I've followed all the instructions several times.
I'm using C++11, GCC, Ubuntu 15.04, and Code::Blocks 13.12.
I am creating a custom static library. At the moment, it contains only two files: iochannel.hpp
and iochannel.cpp
. I compile, and all is well. A single libpawlib.a
file is produced in pawlib/bin/Debug
Next, in the project that I want to use the static library in, I go into Project > Build Options...
. I add the path to the .a file to Linker settings
, and I add the folder the .a file is found in to Search Directories > Compiler
and > Linker
I have checked, double-checked, tripe-checked, and quadruple-checked the paths.
As long as I don't try to import from the library, build of that project is successful, and it runs. However, no matter HOW I put the #include
statement, it says "No such file or directory" when I compile. I have tried all of the following (individually)...
#include <iochannel>
#include <iochannel.hpp>
#include <pawlib/iochannel>
#include <pawlib/iochannel.hpp>
#include "iochannel"
#include "iochannel.hpp"
#include "pawlib/iochannel"
#include "pawlib/iochannel.hpp"
There is absolutely no more instruction online than what I've already followed, as far as I can tell.
This is such a theoretically trivial thing, but I'm stumped. Can anyone help me get this working?
EDIT: My file structure is as follows. In one folder (actually, the root of my repository for this project), I have two folders: pawlib
and pawlib-test
. It contains the following important files...
pawlib/bin/Debug/libpawlib.a
pawlib/src/iochannel.cpp
pawlib/include/iochannel.hpp
The project in pawlib-test
is using the relative path ../pawlib/bin/Debug/libpawlib.a
or ../pawlib/bin/Debug
, depending on if it wants a file or a directory path. The absolute path fails as well. I've confirmed the files and folders exist at those paths.
IMPORTANT: I should not have to include pawlib's .hpp files manually, as that defeats the entire purpose of a library. I use static libraries regularly, and I never have to add the .hpp files or their directory to search directories.
The gcc command and its error (as produced by CodeBlocks) is as follows...
-------------- 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))
EDIT 2: My mistake (partly)...headers are usually imported separately from the static library. The problem is, virtually none of the tutorials or documentation mention this. It's one of those "everyone knows, so it should go without saying" type of things.
Upvotes: 1
Views: 2121
Reputation: 6898
I got confused between static and dynamic libraries. When you are linking to a static library, you must ALSO link to the header files thereof. This is why you find both the /usr/lib
(for static libraries) and /usr/include
(for their headers) folders on any UNIX machine.
As francis pointed out, this has to do with the fact that I hadn't given the path to the header files. If you're on CodeBlocks, go to the Build Options-> Search directories -> Compiler
dialog, and add the path to the folder with the .h
or .hpp
files.
Now it's working just peachy.
Upvotes: 1