I am trying to include the following headers:
#include <libs/serialization/example/portable_binary_iarchive.hpp>
#include <libs/serialization/example/portable_binary_oarchive.hpp>
These files are located in a path like:
/home/nobody/boost_1_45_0/libs/serialization/example/portable_binary_iarchive.hpp
In my Makefile, I have added:
-I/home/nobody/boost_1_45_0/libs
However, when I compile, I get the error messages like:
error: libs/serialization/example/portable_binary_iarchive.hpp: No such file or directory
Can anybody tell me what I am doing wrong here? I am also including boost libraries like
#include <boost/archive/binary_oarchive.hpp>
However, to get those, it is sufficient to do in my Makefile:
-I/usr/include/boost
Why doesn't this work for the headers in the other location? How should I change my Makefile? The first statement current looks like this:
test: test.o
g++ -O3 -ffast-math -funroll-loops -ansi -pedantic-errors -L/usr/lib -lboost_filesystem -lboost_serialization -lboost_iostreams -lz -I/usr/include/boost -I/home/nobody/boost_1_45_0/libs -o test test.o
To get
#include <libs/serialization/example/portable_binary_iarchive.hpp>
from directory
/home/nobody/boost_1_45_0/libs/serialization/example/portable_binary_iarchive.hpp
your Makefile needs
-I/home/nobody/boost_1_45_0
Notice that I omitted the /libs from the end. That's because your #include directive already lists that directory.
As for your second example, is the file you want at this location:
/usr/include/boost/boost/archive/binary_oarchive.hpp
^^^^^ (repeated boost here)
If not g++ is likely defaulting to /usr/include as the search space for
#include <boost/archive/binary_oarchive.hpp>
Ie., your
-I/usr/include/boost
is useless to the compiler.
Related
There are similar questions but their answers did not work for my issue.
I have a c++ program with #include <boost/test/unit_test.hpp> on top (among other includes).
To compile correctly, if I understood, I should do the command:
g++ -g -L/path_to_boost_lib -lboost_lib myprog.cpp -o myprog.exe
If i do a locate, I get /usr/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.
Hence I edited my call to g++ by doing:
g++ -g -L/usr/lib/x86_64-linux-gnu -lboost_unit_test_framework myprog.cpp -o myprog.exe
But I still get errors of the type undefined reference to boost::unit_test.
I also tried the option -I/usr/include/ which contains the boost folder, without success.
It's because of the order. The GCC linker goes through the artifacts left-to-right, and every unknown symbol it encounters in an object file must be resolved by an artifact occurring afterwards.
The right command is thus:
g++ -g myprog.cpp -L/usr/lib/x86_64-linux-gnu -lboost_unit_test_framework -o myprog.exe
See this answer for a more thorough explanation.
I suggest using a build tool like CMake that takes care of such low-level details for you.
arm-linux-gnueabi-g++ can't compile code that uses the xerces-c parser. Specifically, it can't seem to locate the xerces-c library even when I specify the full path with the I- flag or link it with -lxerces-c. However, when I compile with the generic g++ parser, everything works fine. Moreover, g++ works fine when I move around the xerces-c directories.
The code for g++ compilation:
g++ -pthread -g -c -std=c++0x src/MyFile.cpp -o $(TARGET_DIR)/MyFile.o -lxerces-c
The code for arm-linux-gnueabi-g++:
arm-linux-gnueabi-g++ -pthread -g -c -std=c++0x src/MyFile.cpp -o $(TARGET_DIR)/MyFile.o -lxerces-c
This is the error I receive:
src/myFile.cpp fatal error: xerces/util/PlatformUtils.hpp: No such file or directory
#include <xerces/util/Platform/utils.hpp
compilation terminated
I also tried removing the angle brackets enclosing the xerces library and replacing them with quotations so the path wouldn't get messed up.
So, I was following a simple C++ with SDL tutorial for linux but i encounter some errors on my way.
First of all I'm using Geany and i downloaded the corresponding SDL2 libs, here is the thing:
in my project folder there is a main.cxx file, which i open with geany as i mentioned before:
I included this libraries:
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
First i encountered a pelculiar error, compilation performs sucessfully but when it comes to build i got this error:
main.cxx: undefined reference to `SDL_Init'
After searching a bit i found out that i had to add the tag -lSDL to my geany build options so they would end up being somethinf like this:
Compile:
g++ -Wall -c -lSDL "%f"
Build:
g++ -Wall -o -lSDL "%e" "%f"
But there is a problem, now when I execute the build command i get a:
G ++: error: main: There is no such file or directory
Why am i getting this error, am I including a wrong library or g++ has problems with .cxx files?
I already tried converting between .cxx and .cpp.
Thanks in advance.
g++ -Wall -c -lSDL2 "%f"
There is absolutely no need to specify libraries during compilation phase. Remove -lSDL.
g++ -Wall -o -lSDL2 "%e" "%f"
It invokes compiler, implies linking (no -c or other operation-specific flags), and sets output file name to -lSDL2. That is, linker will output resulting binary in a file named -lSDL2 in current working directory. Then, when it comes what files to link, it goes main, which supposed to be -o main, but since you've broken flags order it is now just ordinary file name that linker will try to link into resulting binary. It so happens that this file doesn't exist.
Long story short, make correct linking line - g++ -o "%e" %f -lSDL2 (libraries comes last, library order is also important).
I am having trouble compiling a program I have written. I have two different files with the same includes but only one generates the following error when compiled with g++
/usr/lib/gcc/x86_64-linux-gnu/4.4.1/../../../../lib/crt1.o: In function `_start':
/build/buildd/eglibc-2.10.1/csu/../sysdeps/x86_64/elf/start.S:109: undefined reference to `main'
collect2: ld returned 1 exit status
The files I am including in my header are as follows:
#include <google/sparse_hash_map>
using google::sparse_hash_map;
#include <ext/hash_map>
#include <math.h>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
using std::priority_queue;
using std::stack;
using std::vector;
using __gnu_cxx::hash_map;
using __gnu_cxx::hash;
using namespace std;
Searching the internet for those two lines hasn't resulted in anything to help me. I would be very grateful for any advice. Thank you
To build two separate programs you need both source files to define main() function.
To build a single program out of two source files - first compile each file with -c options (compile only) - you will get two .o files, then link these files together. Something like this:
$ g++ -Wall -pedantic -ggdb -O -c -o module0.o module0.cpp
$ g++ -Wall -pedantic -ggdb -O -c -o module1.o module1.cpp
$ g++ -Wall -pedantic -ggdb -O -o prog module0.o module1.o
to build binary prog from two source files.
If you need to link with some library, you'll have to point compiler to it's headers with -I and to objects with -L flags, then tell the linker to actually reference the library with -l.
Hope this helps.
You need a main function and you don't have one. If you do have a main function, show more code please.
It looks like main is not defined. Do you have one defined for your second program? Can you post more details about the source body that fails to link?
How can I get precompiled headers working with GCC?
I have had no luck in my attempts and I haven't seen many good examples for how to set it up. I've tried on Cygwin GCC 3.4.4 and using 4.0 on Ubuntu.
I have definitely had success. First, I used the following code:
#include <boost/xpressive/xpressive.hpp>
#include <iostream>
using namespace std;
using namespace boost::xpressive;
// A simple regular expression test
int main()
{
std::string hello("Hello, World!");
sregex rex = sregex::compile( "(\\w+) (\\w+)!" );
smatch what;
if( regex_match( hello, what, rex ) )
{
std::cout << what[0] << '\n'; // Whole match
std::cout << what[1] << '\n'; // First capture
std::cout << what[2] << '\n'; // Second capture
}
return 0;
}
This was just a Hello, World! program from Boost Xpressive. First, I compiled with the -H option in GCC. It showed an enormous list of headers that it used. Then, I took a look at the compile flags my IDE (Code::Blocks) was producing and saw something like this:
g++ -Wall -fexceptions -g -c main.cpp -o obj/Debug/main.o
So I wrote a command to compile the Xpressive.hpp file with the exact same flags:
sudo g++ -Wall -fexceptions -g /usr/local/include/boost/xpressive/xpressive.hpp
I compiled the original code again with the -H and got this output:
g++ -Wall -fexceptions -H -g -c main.cpp -o obj/Debug/main.o
! /usr/local/include/boost/xpressive/xpressive.hpp.gch
main.cpp
. /usr/include/c++/4.4/iostream
.. /usr/include/c++/4.4/x86_64-linux-gnu/bits/c++config.h
.. /usr/include/c++/4.4/ostream
.. /usr/include/c++/4.4/istream
main.cpp
The ! means that the compiler was able to use the precompiled header. An x means it was not able to use it. Using the appropriate compiler flags is crucial. I took off the -H and ran some speed tests. The precompiled header had an improvement from 14 seconds to 11 seconds. Not bad, but not great.
Note: Here's the example. I couldn't get it to work in the post.
BTW: I'm using the following g++:
g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3
Firstly, see the documentation here.
You compile headers just like any other file but you put the output inside a file with a suffix of .gch.
So for example if you precompile stdafx.h you will have a precompiled header that will be automatically searched for called stdafx.h.gch anytime you include stdafx.h
Example:
stdafx.h:
#include <string>
#include <stdio.h>
a.cpp:
#include "stdafx.h"
int main(int argc, char**argv)
{
std::string s = "Hi";
return 0;
}
Then compile as:
> g++ -c stdafx.h -o stdafx.h.gch
> g++ a.cpp
> ./a.out
Your compilation will work even if you remove stdafx.h after step 1.
The -x specifier for C++ precompiled headers is -x c++-header, not -x c++. Example usage of PCH follows.
pch.h:
// Put your common include files here: Boost, STL as well as your project's headers.
main.cpp:
#include "pch.h"
// Use the PCH here.
Generate the PCH like this:
$ g++ -x c++-header -o pch.h.gch -c pch.h
The pch.h.gch must be in the same directory as the pch.h in order to be used, so make sure that you execute the above command from the directory where pch.h is.
Call GCC the same way as if you call it for your source file, but with a header file.
E.g.,
g++ $(CPPFLAGS) test.h
This generates a file called test.h.gch.
Every time GCC searches for test.h, it looks first for test.h.gch and if it finds it it uses it automatically.
More information can be found under GCC Precompiled Headers.
I have managed to get precompiled headers working under gcc once in the past, and I recall having problems then as well. The thing to remember is that gcc will ignore the file (header.h.gch or similar) if certain conditions are not met, a list of which can be found on the gcc precompiled header documentation page.
Generally it's safest to have your build system compile the .gch file as a first step, with the same command line options and executable as the rest of your source. This ensures the file is up to date and that there are no subtle differences.
It's probably also a good idea to get it working with a contrived example first, just to remove the possibility that your problems are specific to source code in your project.
Make sure to -include your_header.h
This is how I precompiled and used bits/stdc++.h collection.
Code
#include <bits/stdc++.h>
Then I located the lib by compiling my file with -H and looking at output
g++ sol.cpp -H -O3 -pthread -lm -std=c++14 -o executable
where I saw
. /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h
So I made a new directory bits inside of current one and copied stdc++.h from there.
Then I ran
g++ bits/stdc++.h -O3 -std=c++14 -pthread
which generated bits/stdc++.gch
Normally I compiled my code via
g++ sol.cpp -O3 -pthread -lm -std=c++14 -o executable
, but I had to modify that to
g++ sol.cpp -include bits/stdc++.h -O3 -pthread -lm -std=c++14 -o executable
as it only resolved to .gch file instead of .h with -include bits/stdc++.h
That was key for me. Other thing to keep in mind is that you have to compile *.h header file with almost the same parameters as you compile your *.cpp. When I didn't include -O3 or -pthread it ignored the *.gch precompiled header.
To check if everything's correct you can measure time difference via comparing result of
time g++ sol.cpp ...
or run
g++ sol.cpp -H -O3 -pthread -lm -std=c++14 -o executable
again and look for header paths and if you now get ! before library path, for example
! ./bits/stdc++.h.gch
....
A subtle tip about the file extension that tripped me up, because I wasn't paying close enough attention: the .gch extension is added to the precompiled file's full name; it doesn't replace .h. If you get it wrong, the compiler won't find it and silently does not work.
precomp.h => precomp.h.gch
Not:
precomp.h => precomp.gch
Use GCC's -H to check if it's finding/using it.