简体   繁体   中英

Compile error for simple function inside of namespace in static library

I'm running into a weird challenge: My static library builds and can be used without these logging functions, but when I include them I cannot compile code that includes dove.h and then links against libdove.a. I originally moved all of the log functions outside of the dove namespace and declared/defined them inside dove.cpp, and that worked. However, it leaks the function signatures and I would like to reuse them in an independent project.

dove.h:

namespace dove {
  /* Many functions and classes */

  void log(const char* msg, int level); 
  void info(const char* msg);                                                                                                 
  void error(const char* msg);
  void debug(const char* msg);
}

dove.cpp:

void dove::log(const char* msg, int level) {
  if (level <= LOG_LEVEL)
    std::cout << "dove: " << msg << std::endl;
}

void dove::info(const char* msg) { log(msg, LOG_INFO); }
void dove::error(const char* msg) { log(msg, LOG_ERROR); }
void dove::debug(const char* msg) { log(msg, LOG_DEBUG); }

dove Makefile section:

all:                                                                                                                            
  $(CXX) -c $(CFLAGS) $(INC) -o dove.o dove.cpp
  ar rvs libdove.a dove.o      
  ranlib libdove.a

All of these build completely fine! I get libdove.a and everything seems happy. However, when I have a line #include "dove.h" in a different project, I get the following compile error on dove.h:

make[1]: Entering directory `<omitted>/dove'
g++ -c -g  -Ilibs/rapidxml-1.13 -o dove.o dove.cpp
ar rvs libdove.a dove.o
ar: creating libdove.a
a - dove.o
ranlib libdove.a
make[1]: Leaving directory `<omitted>/dove'
cd <omitted> && make
make[1]: Entering directory `<omitted>'
g++  -g  -c -o build/graph.o src/utils/graph.cpp
g++  -g  -c -o build/util.o src/utils/util.cpp
g++ -g  -I<omitted>/dove -Isrc/utils -c -o build/mps.o src/mps.cpp 
In file included from src/mps.cpp:13:
<omitted>/dove/dove.h:247: error: expected ‘,’ or ‘...’ before string constant
<omitted>/dove/dove.h:250: error: expected ‘,’ or ‘...’ before string constant
make[1]: *** [bin/hybrid] Error 1

These errors always happen on log and debug. Never on info and error. I've placed them in multiple places in the dove namespace (top, middle, separated declarations, bottom) and these two are always complaining.

Makefile for sub-project:

# Contains libdove.a
DOVE_ROOT    ?= $(CURDIR)/../../dove
LIBS         := -L$(DOVE_ROOT) -ldove
INC          := -I$(DOVE_ROOT) -Isrc/utils                                                                                      
CXXFLAGS     += -g  

all: $(util_o)
  $(CXX) $(CXXFLAGS) $(INC) -c -o build/mps.o src/mps.cpp 
  $(CXX) $(CXXFLAGS) -o bin/hybrid build/*.o $(LIBS)

Following comments to the original question, it would appear that there is a conflict between some macros and the function names used in the relevant code.

This can be determined by the fact that the error message doesn't make sense for the actual line of code - there is no "string constant" in the relevant line.

The fix is either to #undef log and #undef debug or to use different names for either the macros or the functions.

[Another argument for not using lower-case macro names!]

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