简体   繁体   中英

Why is the makefile returning g++: error: h file or directory make: *** [Makefile:12: test] Error 1?

I want to compile this program test.cpp

#include <iostream>
#include <cstring>
#include <iomanip>
#include <sodium.h>
#include <gmpxx.h>
#include <sstream>

using namespace std;

//Encryt or  decrypt with RSA
mpz_class RSA(mpz_class m, mpz_class e,mpz_class N)
{
mpz_class rop;
mpz_powm(rop.get_mpz_t(), m.get_mpz_t(), e.get_mpz_t(), N.get_mpz_t());
return rop;
}

int main(const int argc, const char *const argv[])
{
if (argc!=4){
printf("usage: %s [Message] [Exponent] [Modulus] \n", argv[0]);
    return 1;
    }

const mpz_class m(argv[1]), d(argv[2]),N(argv[3]);
cout<<endl<<RSA(m,d,N);

return 0;
}

with this makefile Makefile

CXX = g++
CXXFLAGS = -c -Wall -Wextra -Werror
LDFLAGS =  -lgmp -lsodium -lssl -lcrypto -lgmpxx
SRC = $(wildcard *.cpp )
HDR = $(wildcard *.h )
OBJ = $(SRC :.cpp =.o )
all : Release
Debug : CXXFLAGS +=-g
Debug : test
Release : test
test : $(OBJ)
    $(CXX) -o $@ $ˆ $(LDFLAGS)
%.o : %.cpp $(HDR)
    $(CXX) $(CXXFLAGS) $< -o $@
clean :
    rm -f $(OBJ) test

But I receive

-------------- Build: Debug in KA-RMP (compiler: GNU GCC Compiler)---------------

Checking if target is up-to-date: make -q -f Makefile Debug Running command: make -f Makefile Debug

g++: error: h file or directory

make: *** [Makefile:12: test] Error 1

g++ -o test lsodium -lssl -lcrypto -lgmpxx

Process terminated with status 2 (0 minute(s), 0 second(s)) 2 error(s), 0 warning(s) (0 minute(s), 0 second(s))

Do you have any idea why I receive the error?

The problem is that $^ and $@ is not working as you expected. $@ and $^ return the left and right values of $OBJ. However $OBJ simply resolves to "test.o".

OBJ = $(SRC:.cpp=.o) # converts test.cpp to test.o

Therefore $^ will return nothing because there is nothing on the right side. If you want to use $^ you have to set OBJ to "test.o test.cpp". I made the changes with your makefile.

Otherwise use $(CXX) -o $@ $(SRC) $(LDFLAGS).

CXX = g++
CXXFLAGS = -c -Wall -Wextra -Werror
LDFLAGS =  -lgmp -lsodium -lssl -lcrypto -lgmpxx
SRC = $(wildcard *.cpp )
HDR = $(wildcard *.h )
OBJ = $(SRC) $(SRC :.cpp =.o )
all : Release
Debug : CXXFLAGS +=-g
Debug : test
Release : test
test : $(OBJ)
    $(CXX) -o $@ $^ $(LDFLAGS)
%.o : %.cpp $(HDR)
    $(CXX) $(CXXFLAGS) $< -o $@
clean :
    rm -f $(OBJ) test

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM