简体   繁体   English

如何在一个项目中使用gcc编译c文件和使用g++编译hpp

[英]how to compile c file using gcc and compile hpp using g++ in one project

I have C project needs to link opencv library, the c code must compile with gcc, if I compile the project using gcc then it report errors that opencv hpp header file must be compiled as C++. so, any possible to compile c files using gcc and compile hpp files using g++ in one project? I have C project needs to link opencv library, the c code must compile with gcc, if I compile the project using gcc then it report errors that opencv hpp header file must be compiled as C++. so, any possible to compile c files using gcc并在一个项目中使用 g++ 编译 hpp 文件? or any way to solve my compile problem或任何解决我的编译问题的方法

I specified rules below in makefile,but it seems not work我在 makefile 中指定了以下规则,但似乎不起作用

$(OBJ_DIR)/%.o: %.hpp
    @echo "  COMPILE $(abspath $<)"
    @mkdir -p $(OBJ_DIR)
    @$(CXX) -c $(CFLAGS) -o $@ $<

$(OBJ_DIR)/%.o: %.c 
    @echo "  COMPILE $(abspath $<)"
    @mkdir -p $(OBJ_DIR)
    @$(CC) -c $(CFLAGS) -o $@ $<

$(OBJ_DIR)/%.o: %.cpp
    @echo "  COMPILE $(abspath $<)"
    @mkdir -p $(OBJ_DIR)
    @$(CXX) -c $(CFLAGS) -o $@ $<

$(OBJ_DIR)/%.o: %.cc
    @echo "  COMPILE $(abspath $<)"
    @mkdir -p $(OBJ_DIR)
    @$(CXX) -c $(CFLAGS) -o $@ $<
$(OBJ_DIR)/%.o: %.c 
    @echo "  COMPILE $(abspath $<)"
    @mkdir -p $(OBJ_DIR)
    @$(CC) -c $(CFLAGS) -o $@ $<

Looks OK.看起来不错。

$(OBJ_DIR)/%.o: %.cpp
    @echo "  COMPILE $(abspath $<)"
    @mkdir -p $(OBJ_DIR)
    @$(CXX) -c $(CFLAGS) -o $@ $<

Not OK.不好。 That should be CXXFLAGS (for C++), not CFLAGS (for C).那应该是CXXFLAGS (对于 C++),而不是CFLAGS (对于 C)。

$(OBJ_DIR)/%.o: %.cc
    @echo "  COMPILE $(abspath $<)"
    @mkdir -p $(OBJ_DIR)
    @$(CXX) -c $(CFLAGS) -o $@ $<

Same as above.同上。 Plus, your project should use either .cc or .cpp, not both.另外,您的项目应该使用.cc.cpp,而不是两者。

$(OBJ_DIR)/%.o: %.hpp
    @echo "  COMPILE $(abspath $<)"
    @mkdir -p $(OBJ_DIR)
    @$(CXX) -c $(CFLAGS) -o $@ $<

.hpp files include C++ code, so that is where the error message comes from. .hpp 文件包含 C++ 代码,因此这是错误消息的来源。 But more importantly, header files are included , not compiled to object code, so this whole make rule makes no sense.但更重要的是,包含了 header 个文件,没有编译成 object 代码,所以整个 make 规则没有意义。 Your project should work without it (unless you have a different problem elsewhere).你的项目应该没有它就可以工作(除非你在其他地方有不同的问题)。


Your comments indicate that you are somewhat arbitrarily mixing C code compiled with GCC, C code compiled with G++, and C++ code compiled with G++. This is a bad idea and will give you all kinds of problems down the road.您的评论表明您在某种程度上任意混合使用 GCC 编译的 C 代码、使用 G++ 编译的 C 代码和使用 G++ 编译的 C++ 代码。这是一个主意,会给您带来各种问题。 C++ and C are somewhat compatible, but different in minor details that can and will come back and bite you. C++ 和 C 在某种程度上是兼容的,但在细节上有所不同,这些细节可能并且反咬你一口。 If it's C, compile it as C. If it's C++, compile it as C++. If you want to interface the two, write interfacing code.如果是C,则编译为C。如果是C++,则编译为C++。如果要将两者连接起来,请编写连接代码。 Don't just mix the codebases.不要只是混合代码库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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