In my project files I just want to be able to say:
main.cpp:
#include <foo.h>
#include <bar.h>
When these headers files reside in separate
-Project
-include
-foo
foo.h
-bar
bar.h
-src
main.cpp
I've setup my make file to attempt to achieve this but I still get fatal error: foo.h: No such file or directory so I haven't been able to set it up correctly.
Makefile:
LIBS = ./include/foo ./include/bar
all:
g++ -o bin/myapp src/main.cpp $(LIBS) -std=c++11
Is LIBS correct? How can I achieve relative/agnostic include paths?
INCLUDES = -I./include/foo -I./include/bar
all:
g++ -o bin/myapp src/main.cpp $(INCLUDES) -std=c++11
Related
I'm trying to compile a small .cpp file but running into issues since this file utilizes functions found in many other files. Basically, I cloned the ZCash repo and am currently trying to use specific files in that project to do some testing. When I compile the entire ZCash repo that I cloned everything works perfectly, but when I try to compile the files I'm working with some of them get syntax errors.
My Makefile:
CC = g++
CFLAGS = -std=c++11 -Wall -g -c
IDIR = /home/parallels/zcash/src/
INC = -I /home/parallels/zcash/src/ -I
/home/parallels/zcash/depends/x86_64-unknown-linux-gnu/include/
VPATH = /home/parallels/zcash/src/
all: Main.o KeyGen.o prf.o uint256.o sha256.o
$(CC) Main.o KeyGen.o prf.o uint256.o sha256.o -o keygenerator
Main.o: Main.cpp
$(CC) $(CFLAGS) Main.cpp $(INC)
KeyGen.o: KeyGen.cpp
$(CC) $(CFLAGS) KeyGen.cpp $(INC)
prf.o: prf.cpp
$(CC) $(CFLAGS) prf.cpp $(INC)
uint256.o: uint256.cpp
$(CC) $(CFLAGS) uint256.cpp $(INC)
sha256.o: sha256.cpp
$(CC) $(CFLAGS) sha256.cpp $(INC)
clean:
rm -rf *.o keygenerator
The .cpp files being compiled reference these other files:
Main.cpp:
#include "KeyGen.h"
#include "uint252.h"
#include "uint256.h"
#include <string>
KeyGen.cpp
#include "KeyGen.h"
#include "prf.h"
prf.cpp
#include "prf.h"
#include "crypto/sha256.h"
uint256.cpp
#include "uint256.h"
#include "utilstrencodings.h"
#include <stdio.h>
#include <string.h>
sha256.cpp
#include "crypto/sha256.h"
#include "crypto/common.h"
#include <string.h>
#include <stdexcept>
So, after I try to compile these files I get the following error:
g++ -std=c++11 -Wall -g -c Main.cpp -I /home/parallels/zcash/src/ -I /home/parallels/zcash/depends/x86_64-unknown-linux-gnu/include/
g++ -std=c++11 -Wall -g -c KeyGen.cpp -I /home/parallels/zcash/src/ -I /home/parallels/zcash/depends/x86_64-unknown-linux-gnu/include/
g++ -std=c++11 -Wall -g -c prf.cpp -I /home/parallels/zcash/src/ -I /home/parallels/zcash/depends/x86_64-unknown-linux-gnu/include/
g++ -std=c++11 -Wall -g -c uint256.cpp -I /home/parallels/zcash/src/ -I /home/parallels/zcash/depends/x86_64-unknown-linux-gnu/include/
g++ -std=c++11 -Wall -g -c sha256.cpp -I /home/parallels/zcash/src/ -I /home/parallels/zcash/depends/x86_64-unknown-linux-gnu/include/
In file included from /usr/include/x86_64-linux-gnu/bits/byteswap.h:35:0,
from /usr/include/endian.h:60,
from /usr/include/x86_64-linux-gnu/bits/waitstatus.h:64,
from /usr/include/stdlib.h:42,
from /home/parallels/zcash/src/crypto/sha256.h:9,
from sha256.cpp:5:
/home/parallels/zcash/src/compat/endian.h:111:17: error: expected unqualified-id before ‘__extension__’
inline uint16_t htobe16(uint16_t host_16bits)
^
In the offending file /home/parallels/zcash/src/compat/endian.h:111:17, htobe16 is used as follows:
#if HAVE_DECL_HTOBE16 == 0
inline uint16_t htobe16(uint16_t host_16bits) //line #111
{
return bswap_16(host_16bits);
}
#endif // HAVE_DECL_HTOBE16
There also exists a file: /usr/include/endian.h who references htobe16 as follows.
# include <bits/byteswap.h> //line #60
# if __BYTE_ORDER == __LITTLE_ENDIAN
# define htobe16(x) __bswap_16 (x)
# define htole16(x) (x)
# define be16toh(x) __bswap_16 (x)
# define le16toh(x) (x)
Note:
All the .cpp file I am compiling are located in both the ZCash repo that I cloned and the new folder I created (wondering if this might be the issue).
The folder I'm currently working on contains :
KeyGen.cpp
KeyGen.h
Main.cpp
Makefile
prf.cpp
prf.h
serialize.h
sha256.cpp
sha256.h
uint252.h
uint256.cpp
uint256.h
Sorry for the long post, I truly appreciate your time and would really appreciate some help!
The inline functions are already declared by several macros with the same name. Similar to this question: error: expected unqualified-id before ‘__extension__’ in Linux (Cent OS)
Could have to do with a unconfigured part of the slice you're building from the whole project as this Github discussion seems to indicate:
https://github.com/bitcoin/bitcoin/issues/5920
I've been learning c++ and encountered the following question:
I have a directory structure like:
- current directory
- Makefile
- include
- header.h
- src
- main.cpp
my header.h :
#include <iostream>
using namespace std;
void print_hello();
my main.cpp:
#include "header.h"
int main(int argc, char const *argv[])
{
print_hello();
return 0;
}
void print_hello()
{
cout<<"hello world"<<endl;
}
my Makefile:
CC = g++
OBJ = main.o
HEADER = include/header.h
CFLAGS = -c -Wall
hello: $(OBJ)
$(CC) $(OBJ) -o $#
main.o: src/main.cpp $(HEADER)
$(CC) $(CFLAGS) $< -o $#
clean:
rm -rf *o hello
And the output of make is:
g++ -c -Wall src/main.cpp -o main.o
src/main.cpp:1:20: fatal error: header.h: No such file or directory
compilation terminated.
Makefile:10: recipe for target 'main.o' failed
make: *** [main.o] Error 1
What mistakes I have made in here. It's frustrating. Really appreciate any advice!
You told the Makefile that include/header.h must be present, and you told the C++ source file that it needs header.h … but you did not tell the compiler where such headers live (i.e. in the "include" directory).
Do this:
CFLAGS = -c -Wall -Iinclude
You can either add a -I option to the command line to tell the compiler to look there for header files. If you have header files in include/ directory, then this command should work for you.
gcc -Iinclude/
Since, you are using makefile, you can include this option in CFLAGS macro in your makefile.
CFLAGS = -Iinclude/ -c -Wall
OR
You can include header files using #include "../include/header.h".
Perhaps change your include line:
#include "include/header.h"
Assuming that's where your header exists - I'm making that assumption from your makefile
A Makefile in the subdirectory of my project doesn't see the include path ONLY when it is used from the main Makefile of my project. I don't have much experience with Makefiles and a lot of what I've been reading is pretty confusing. Here is the layout of my directory (with what I have so far since I just started the project):
main/
Inventory/
Item.h
Item.cpp
Makefile
tools/
include/
json/
json.h
jsoncpp.cpp
Makefile
main.cpp
Makefile
Here is the Makefile in the main directory:
INCLUDE = -IInventory/
CC = g++
DEP = tools/jsoncpp.o Inventory/Item.o
Main: main.o $(DEP)
cd tools/ && make
cd Inventory/ && make
$(CC) -o Main main.o $(DEP) $(INCLUDE)
main.o main.cpp
$(CC) -c main.cpp $(INCLUDE)
Here is the Makefile in the tools directory:
INCLUDE = -Iinclude/
CC = g++
jsoncpp.o: jsoncpp.cpp
$(CC) -c jsoncpp.cpp $(INCLUDE)
When I call make from the tools/, it works just fine. But when I call make from the main directory I get this error:
g++ -c -o tools/jsoncpp.o tools/json.cpp
tools/jsoncpp.cpp:76:23: fatal error: json/json.h: No such file or directory
#include "json/json.h"
^
compilation terminated.
Now I partially believe that it can't find the include directory for whatever reason, but the first line in that error is fairly odd to me because of that weird gap between g++ and -c. Since my project will soon get pretty big, how can I fix this?
If it's in -I directive, it should be #include <json/json.h> otherwise #include "include/json/json.h"
EDIT: Include directory is taken from current directory, so in main/ you have to use -Itools/include
Solution: Implicit rules were used so correct variable CXXFLAGS+=$(INCLUDE) must be set for compilation. See: make manual
And the main problem is Main: main.o $(DEP) - files in DEP must exist already otherwise it'll use implicit rules. Later after that cd tools/ && make is done.
#include <json/json.h> <- preprocessor will search in all includes paths
see gcc Include Syntax :
#include <file> This variant is used for system header files. It searches for a file named file in a standard list of system
directories. You can prepend directories to this list with the -I
option (see Invocation).
#include "file" This variant is used for header files of your own program. It searches for a file named file first in the directory
containing the current file, then in the quote directories and then
the same directories used for . You can prepend directories to
the list of quote directories with the -iquote option.
Think the following example
myproj/
main.cc
test.h
main.cc simple include test.h with
#include "myproj/test.h"
where I run g++ -MM and -M in the same dir of myproj, I get
$ g++ -MM myproj/main.cc
main.o: myproj/main.cc
$ g++ -M myproj/main.cc
main.o: myproj/main.cc /usr/include/stdc-predef.h myproj/test.h
you will see that -MM option ignore the header myproj/test.h
The Question is myproj/test.h is not the system header, why -MM option ignore it.
In include you specify path to header relatively the current source file(main.cc in your case). You need just
#include "test.h"
I'm including the freetype2 library by calling the freetype2/ft2build.h header file.
#include <freetype2/ft2build.h>
The header file itself calls another header file in a subdirectory.
#include <config/ftheader.h>
Which causes the 'make' to fail.
/usr/include/freetype2/ft2build.h:37:29: fatal error: config/ftheader.h: No such file or directory
#include <config/ftheader.h>
The 'config' subdirectory is within the 'freetype2', but the compiler is clearly looking for it in the /usr/include directory.
I cannot find a solution to this, but I can't possibly be the only one it affects. What am I missing?
I've tried adding INCLUDE=-I/usr/include/freetype2/config to the makefile but this doesn't work.
Makefile is as follows
INCLUDE=-I/usr/include/freetype2
CC=g++
LDLIBS=-lglut -lGLEW -lGL
all: main
clean:
rm -f *.o main
.PHONY: all clean
I think you need to replace
#include <freetype2/ft2build.h>
with
#include <ft2build.h>
and use
INCLUDE=-I/usr/include/freetype2
in makefile
Changed my Mkefile.
CPPFLAGS=-I/usr/include/freetype2 -I/usr/include/freetype2/config
CC=g++
LDLIBS=-lglut -lGLEW -lGL
all: main
clean:
rm -f *.o main
.PHONY: all clean
from INCLUDE to CPPFLAGS.