Trouble compiling a program using an archive (.a) - c++

When I try to compile my file using a library (.a), I get 'fatal error: URLInputStream.h: No such file or directory
compilation terminated.
'. I'm still pretty new to C++, and this seems so simple but I can't get it to work.
Compilation commands I've tried:
g++ inc/Downloader.h lib/libcs240utils.a
g++ inc/Downloader.h -L lib -l cs240utils
g++ inc/Downloader.h -Llib -lcs240utils
g++ src/Downloader.cpp -I inc -L lib -l cs240utils
g++ -c src/Downloader.cpp -I inc -L lib -l cs240utils
How I compile my archive:
make lib
g++ -c -o utils/obj/CommandRunner.o utils/src/CommandRunner.cpp -I utils/inc
g++ -c -o utils/obj/FileInputStream.o utils/src/FileInputStream.cpp -I utils/inc
g++ -c -o utils/obj/FileSystem.o utils/src/FileSystem.cpp -I utils/inc
g++ -c -o utils/obj/HTMLToken.o utils/src/HTMLToken.cpp -I utils/inc
g++ -c -o utils/obj/HTMLTokenizer.o utils/src/HTMLTokenizer.cpp -I utils/inc
g++ -c -o utils/obj/HTTPInputStream.o utils/src/HTTPInputStream.cpp -I utils/inc
g++ -c -o utils/obj/StringUtil.o utils/src/StringUtil.cpp -I utils/inc
g++ -c -o utils/obj/URLInputStream.o utils/src/URLInputStream.cpp -I utils/inc
ar cr lib/libcs240utils.a utils/obj/*.o
The archive seems to be built correctly:
ar t lib/libcs240utils.a
CommandRunner.o
FileInputStream.o
FileSystem.o
HTMLToken.o
HTMLTokenizer.o
HTTPInputStream.o
StringUtil.o
URLInputStream.o
I've also tried various options in ar. If I specify an include path -I utils/inc then it will compile and work properly, so the file I want does work properly. I've read numerous articles and questions here on StackOverflow, and I can't seem to see what I'm doing incorrectly. Any ideas or suggestions?
Note: I'm compiling a header which seems weird. It was originally split into .cpp and .h, but to simplify problem solving I merged them. This same error happened when they were split as well: g++ src/Downloader.cpp -I inc -L lib -l cs240utils

If Downloader.h still likely needs header files to talk to types/classes/etc. in your static library.
The fact that you mention that "-I utils/inc" makes it work would lead me to think that Downloader.h references stuff in that header. You still need the header for compilation even when you have a static lib as part of the link step.

Related

Cannot link the header file using gcc linker in windows

hello.c will use the header file plugin.h. If I want to use gcc to compile, can I know how should I write the -LXXX.
gcc -o hello hello.c -LXXX
Currently my project structure look like this
project directory is (C:\project)
project
examples/ hello.c
include / plugins/ plugin.h
You don't link the header file, you include it. You link object files and static/shared libraries, which are already compiled files.
But to answer your question, you include your plugin with the -I option.
g++ -O2 examples/hello.c -I include/plugins -o hello
Or if you have a library to link say in lib:
g++ -O2 examples/hello.c -I include/plugins -L lib/plugins -lplugin -o hello
Or if you want to do in two steps (notice the -c)
g++ -O2 -c examples/hello.c -I include/plugins -o hello.o
g++ hello.o -L lib/plugins -lplugin -o hello

g++ creates shared object with dynamic dependency even though I gave it a static archive in the inputs

I have a source file, mything.cpp, and a library that was provided to me as notmine.h and notmine.a.
I need to produce a shared object that has all my stuff from mything.cpp and all the stuff from somelib.a. Here is what I did on the command line:
g++ -fpic -c -o mything.o mything.cpp
g++ -shared -o mything.so mything.o notmine.a
However, when I look at the final mything.so using ldd I see that it has a dependency on libnotmine.so, and when I check nm, I see that all the symbols that should have been supplied by notmine.a are undefined.
What am I doing wrong?
More details: notmine.a is actually liblua.a that I built locally. I think g++ might be getting confused because there is a liblua.so in the system directories
Finally figured it out. There are two options. The simpler is to use:
g++ -fpic -c -o mything.o mything.cpp
g++ -shared -o mything.so mything.o -L. -l:notmine.a
Alternatively, you can tell the linker you want to treat the .a as a bunch of object files with
g++ -fpic -c -o mything.o mything.cpp
g++ -shared -o mything.so mything.o -Wl,--whole_archive notmine.a -Wl,--no-whole-archive
The --Wl,-no-whole-archive is to prevent that flag from messing up the other steps the linker does with the system libraries

Can I merge these g++ calls into one?

g++ -I include/ -I include/gliese/ -I include/othello/ -c src/OthelloHeuristics.cpp -o obj/Debug/src/OthelloHeuristics.o
g++ -I include/ -I include/gliese/ -I include/othello/ -c src/OthelloNegamaxSearch.cpp -o obj/Debug/src/OthelloNegamaxSearch.o
g++ -I include/ -I include/gliese/ -I include/othello/ -c src/main.cpp -o obj/Debug/src/main.o
g++ -L lib -l othello -l SDL_image -l SDL_ttf -l SDL obj/Debug/src/main.o obj/Debug/src/OthelloHeuristics.o obj/Debug/src/OthelloNegamaxSearch.o -o bin/Debug/othello
Currently I'm running those lines in order to build a game, and it works. But is there anyway to merge them into one? (in order to gain some speed). Thanks!
you can write like this
g++ -I include/gliese/ -c src/OthelloHeuristics.cpp src/OthelloNegamaxSearch.cpp src/main.cpp
g++ -L lib -l othello -l SDL_image -l SDL_ttf -l SDL obj/Debug/src/*.o -o bin/Debug/othello

How do I link multiple files without the use of a makefile?

For example, I'm given carModels.cpp, carModels.h, carType.in, manufacturers.h, manufacturers.o, and lastly my own file tester.cpp. How would I go about linking all of these using g++ in a Linux terminal? Would I have to create any additional ".o" files? I'm supposed to assume that the given files already work. Multiple lines in terminal are fine, I just I want a clear understanding of it. (I'm coming from a C++ IDE that I didn't really care for.)
Compile each source file to its own object file:
g++ -I . -c carModels.cpp -o carModels.o
g++ -I . -c tester.cpp -o tester.o
Now link all object files together:
g++ carModels.o tester.o manufacturers.o -o outputname
Consider adding more options like -O3, -std=c++11, -Wall, etc. as needed.
you can do this in two steps, first compile to *.o files,
gcc -c your.cpp other.cpp .....
then link them
gcc -o you_out_put_name the_object_files.o ...
In a single line, that would be just g++ -o tester *.cpp *.o. GCC will sort everything out. In particular, the *.h files are referenced via #include "" statements in the .cpp files.

makefiles and linking a library in a different folder

I've search around a bit on StackOverflow and tried a few suggestions but as of yet nothing has solved the problem.
I'm making a makefile for a school project and as part of my project I'm generating a static library and linking against it. The compiler throws an error when it gets to a header include in the static library. The code for that is just #include "StringUtil.h"
So in the makefile I have these relevant parts of code
LINKFLAGS=-Llib/ -lHTMLtools
bin : lib $(BIN_FILE)
lib : $(LIB_OBJ_FILES)
ar r lib/libHTMLtools.a $(LIB_OBJ_FILES)
$(BIN_FILE) : $(OBJ_FILES) #This is only obj/crawler.o for now
g++ -o bin/crawler obj/crawler.o
obj/crawler.o : src/crawler.cpp inc/crawler.h
g++ -c -static $(LINKFLAGS) -o obj/crawler.o -I inc src/crawler.cpp
so whenever I run the make bin command it generates lib.libHTMLtools.a as expected but when it gets to the
g++ -c -static $(LINKFLAGS) -o obj/crawler.o -I inc src/crawler.cpp
line it returns this error.
src/crawler.cpp:2:24: fatal error: StringUtil.h: No such file or directory compilation terminated.
Any help or advice will be appreciated!
In C++, library files are not enough. They are not used when compiling source code, but only when linking. To compile source file, you need to include headers. But the compiler need to know where to find it. Try adding -I utils/inc to your last line like this
g++ -c -static $(LINKFLAGS) -o obj/crawler.o -I inc -I utils/inc src/crawler.cpp