简体   繁体   中英

Static lib in c++ not working

I have folder ClientServer . In this folder I have two other folders, Database and Server . In Server folder I have classes that use "database.h" and "newsgroup.h". These files are in Database folder. I created a lib with this make file. I moved the libfile to ClientServer folder. Then I try to call Make in Server folder; I get error.

Makefile:47: ans.d: No such file or directory
Makefile:47: com.d: No such file or directory
Makefile:47: myserver.d: No such file or directory
In file included from myserver.cc:11:0:
ans.h:4:23: fatal error: newsGroup.h: No such file or directory

#include "newsGroup.h"
                   ^
compilation terminated.


#
# Makefile to make the file libclientserver.a, containing
# connection.o and server.o
#
# Define the compiler. g++ can be
# changed to clang++.
CXX = g++
CC  = g++

# Define preprocessor, compiler, and linker flags. Uncomment the # lines
# if you use clang++ and wish to use libc++ instead of libstdc++.
CXXFLAGS =  -g -O2 -Wall -W -pedantic-errors
CXXFLAGS += -Wmissing-braces -Wparentheses -Wold-style-cast 
CXXFLAGS += -std=c++11 
#CPPFLAGS =  -stdlib=libc++
#CXXFLAGS += -stdlib=libc++
#LDFLAGS += -stdlib=libc++

all: libdatabase.a

# Create the library; ranlib is for Darwin and maybe other systems.
# Doesn't seem to do any damage on other systems.

libdatabase.a: Database.o newsGroup.o
    ar rv libdatabase.a Database.o newsGroup.o 
    ranlib libdatabase.a

# Phony targets
.PHONY: all clean

# Standard clean
clean:
    rm -f *.o libclientserver.a

# Generate dependencies in *.d files
%.d: %.cc
    @set -e; rm -f $@; \
         $(CPP) -MM $(CPPFLAGS) $< > $@.$$$$; \
         sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
         rm -f $@.$$$$

# Include the *.d files
SRC = $(wildcard *.cc)
include $(SRC:.cc=.d)

Actually, a library just holds "the body of the functions".

You still have to declare the prototypes of functions (include .h files), and make it accessible for the compiler.

So, in your case, make newsgroup.h accessible to your compiler (in this case, put it in the same folder as the ans.h file), it should resolve the problem.

Where is newsGroup.h placed in your directory structure? Do the .cpp files see it in the same directory?? Otherwise use the -I option, to tell the compiler in which directory this file can be found.

Add eg -I <path_to>/Database to your CXXFLAGS , where <path_to> might be either a full, or relative path to the working directory used to run the compiler:

CXXFLAGS += -I<path_to>/Database

Another option is to specify a relative path in the #include statements, eg in ans.h write

#include "Database/newsGroup.h"

and have the -I option just point to <path_to>/ .
Since the .d files will receive these paths when generated and specify the dependencies at that point should also see the .h dependencies relative from make 's working directory.

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