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
Related
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.
I have a small project to create in a course at my University that requires using the Crypto++ libraries. The requirement is that we don't include the whole source code/binary files of Crypto++ but link it from an outside directory. (E.g. C:\cryptopp). This is because the reviewer will link his/her own directory to asses my code.
Now, I am really bad at creating Makefiles and don't understand the content of them completely.
I am using MinGW on Windows 7.
So my main question would be, what do I need to write in the Makefile to use Crypto++ in my project from an outside folder?
Suppose you have the following makefile:
unit.exe: unit.o
g++ unit.o -o unit.exe
unit.o: unit.cc unit.h
g++ -c unit.cc -o unit.o
In order to modify it to use an external library you have to use the GCC -I and -L options:
unit.exe: unit.o
g++ unit.o -o unit.exe -L /c/cryptopp -l ws2_32 -l cryptopp
unit.o: unit.cc unit.h
g++ -I /c/cryptopp -c unit.cc -o unit.o
Often a makefile would contain a variable that is passed to the compiler and a variable that is passed to the linker, for example CFLAGS and LDFLAGS. If that is the case, then it might be easier to add the "-I" and "L" options to the compiler and linker variables.
See also here for a way to comiple CryptoPP.
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.
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
i am using Festival c++ Api but in the manual provided at
http://www.cstr.ed.ac.uk/projects/festival/manual/festival_28.html#SEC132
saying to link festival/src/lib/libFestival.a etc.
so please tell me hw to link them with my c++ programme
The simplest way to link a static library from g++ is simply to name the library on the command line, using the complete path:
g++ mycode.cpp -o myprog /myinstall/festival/src/lib/libFestival.a
where /myinstall is wherever you installed the libraries. You can also specify the path and the library with the -L and -l flags:
g++ mycode.cpp -o myprog -L/myinstall/festival/src/lib -lFestival
I assume that you put your file.cpp in the directory containing festival and speech_tools which are extracted from packages.
compile:
g++ yourFile.cpp -o yourFile -I./festival/src/include -I./speech_tools/include -L./festival/src/lib -lFestival -L./speech_tools/lib/ -lestools -lestbase -leststring