0

I am trying to set up syntax checking with flymake and I've got the basic setup working.

My makefile for flymake is simply like follows:

INCLUDES = -I ./inc ## list of more includes omitted for brevity
.PHONY: check-syntax
check-syntax:
    gcc -Wall -Wextra -pedantic -fsyntax-only -Wno-variadic-macros -std=c99 $(INCLUDES) $(CHK_SOURCES)

This works ok for my C sources.

Now how to use the same makefile and the same check-syntax target for C++? I can't set multiple -std options like -std=c99 -std=c++98 to the same gcc invocation or can I? Do I need to use some conditional? I can't have multiple check-syntax targets on the same Makefile.

teroi
  • 1,087
  • 10
  • 19
  • You can make `check-syntax` depend on multiple other targets though: `check-syntax: check-syntax-c check-syntax-cpp` – Joe Z Dec 04 '13 at 14:06

1 Answers1

2

You could split it up into two targets:

check-syntax: check-syntax-c check-syntax-cxx

Then check the syntax using the C compiler and the C flags in one target, and using the C++ and C++ flags for the other.

Although you have to split up your sources in C and C++ sources, which you should do anyway.


If you don't have the files split already, you can do it using a GNU Make text function named filter to split the sources:

CHK_SOURCES_C   = $(filter %.c,$(CHK_SOURCES))
CHK_SOURCES_CXX = $(filter %.cpp,$(CHK_SOURCES))
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Actually the C and C++ are already split into separate folders which have separate makefiles but I would not like to add flymake support to each and everyone of those. Instead I would like to direct the flymake to use just the top level makefile. CHK_SOURCES in that case contains whatever flymake is feeding into it. So it may contain a c or c++ source file name. – teroi Dec 04 '13 at 14:24
  • @teroi Updated the answer with how you can split into C and C++ source files. – Some programmer dude Dec 04 '13 at 14:32
  • Ah, now I see your point. I know the filter function; just don't didn't think this through... That is actually quite clean solution. i'll test drive this. – teroi Dec 04 '13 at 14:41
  • Ok, quite good already but it is trying to build both check-syntax-c and check-syntax-cpp targets even though either the CHK_SOURCES_C or CHK_SOURCE_CXX is empty... – teroi Dec 04 '13 at 14:54
  • @teroi Look at the [conditional functions](https://www.gnu.org/software/make/manual/html_node/Conditional-Functions.html#Conditional-Functions). Define and use a specific target *if* the corresponding source-file macro is non-empty – Some programmer dude Dec 04 '13 at 14:57
  • I ended up using http://www.chemie.fu-berlin.de/chemnet/use/info/make/make_7.html . So no conditional functions but just conditionals. I am accepting the answer since I got to the right direction by the help of that and the additional comments. – teroi Dec 04 '13 at 16:10
  • 1
    It must be also noted that flymake will generate a temporary copy of the file which it will compile. So any .c file will be copied to .c_flymake, for example. So when splitting the sources we'll need to filter CHK_SOURCES with %.c_flymake and %.cpp_flymake patterns. – teroi Dec 04 '13 at 16:47