32

I recently got the following error when trying to compile with gcc:

error: missing binary operator before token "("

Web and SO searches came up with several specific examples of this error, with specific code changes to fix them. But I found no general description of what condition causes this error to be issued.

When and why does gcc emit this error?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
AShelly
  • 34,686
  • 15
  • 91
  • 152

4 Answers4

55

This is not a compiler error, it is a preprocessor error. It occurs when the preprocessor encounters invalid syntax while trying to evaluate an expression in a #if or #elif directive.

One common cause is the sizeof operator in an #if directive:

For example:

  #define NBITS (sizeof(TYPE)*8)
  //later
  #if (NBITS>16)    //ERROR

This is an error because sizeof is evaluated by the compiler, not the preprocesor.

Type casts are also not valid preprocessor syntax:

  #define ALLBITS ((unsigned int) -1)
  //later
  #if (ALLBITS>0xFFFF)    //ERROR

The rules for what can be in a valid expression are here.

Note also that #if will evaluate an undefined macro as 0, unless it looks like it takes arguments, in which case you also get this error:

So if THIS is undefined:

#if THIS == 0  //valid, true

#if THIS > 0 //valid, false

#if THIS() == 0  //invalid. ERROR

Typos in your #if statement can also cause this message.

AShelly
  • 34,686
  • 15
  • 91
  • 152
  • The error message is indeed misleading, and IMHO, the preprocessor should output a more meaningful one for the errors that actually occur in practice. So, I've just reported a bug in the GCC Bugzilla: [Bug 87351 - misleading error message: missing binary operator before token "("](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87351) – vinc17 Sep 18 '18 at 11:20
  • This answer has nothing to do with the question; it's just explaining preprocessor syntax. – Larry_C Jun 04 '20 at 17:57
  • 6
    Um, I wrote both the question and this answer. The question is "What does this error message mean". The answer is "it's a preprocesor syntax error in a conditional, and here are some common examples of syntax that cause it." I wrote this because when I tried to search for the cause of this error message, everything I found were specific examples of changes to make it go away, but no explanation about what the message actually _means_. Feel free to improve the answer, that's what the edit button is for. – AShelly Jun 04 '20 at 23:26
7

If you are on Linux, make sure that you do not have a header named features.h inside your project files. I had one with this name, which resulted in:

/usr/include/x86_64-linux-gnu/bits/huge_val.h:25: error: function pointer expected

or

/usr/include/bits/huge_val.h:26:18: error: missing binary operator before token "("

That is because some system headers like huge_val.h use macros like __GNUC_PREREQ that are defined by /usr/include/features.h (learn more about this header in this SO question).

In my case I first saw this error when I started to use gcc's -I option which suddenly made gcc select my project include directory before the default system include directories.

Gabriel Devillers
  • 3,155
  • 2
  • 30
  • 53
  • 1
    Got a similar issue with a `features` header in Xilinx Vivado HLS C synthesis because of file `/Vivado/2017.4/lnx64/tools/gcc/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.6.3/include-fixed/features.h:339:0` – Patrizio Bertoni Jan 20 '21 at 11:26
1

You get this error sometimes if you have -fno-operator-names in your compiler flags. I suffered from the exact error while building json and this solved it.

-6

check the direct. no space,no special exp: add_subdirectory(Main)->add_subdirectory(main)

  • You can check your folder's name. I had solved the problem by this way. – asdgasg Sep 09 '20 at 10:37
  • 4
    Then I would advise that you a) add more context and explain how you're using CMake, b) improve the grammar and formatting of your answer for clarity. – General Grievance Sep 09 '20 at 11:39
  • Refer to:https://www.crifan.com/cygwin_buildroot_xscale_make_bin_sh_c_line_0_syntax_error_near_unexpected_token_left_parenthesis/ – asdgasg Sep 10 '20 at 02:42