0

Minimal reproducible example:

  1. In CLion, using the MinGW toolchain (version w64 6.0, with g++ 8.1.0), create a new project with the "C++ executable" template
  2. Write the following in CMakeLists.txt:
    cmake_minimum_required(VERSION 3.17)
    project(codeforces)
    
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -x c++")
    
    add_executable(codeforces main.cpp)
    
  3. Write the following program in main.cpp:
    int main() { return 0; }
    
  4. Build the project with the default configuration

At the linking stage, I get a bunch of stray [...] in program errors:

CMakeFiles\codeforces.dir/objects.a:2:59: error: stray '`' in program
 /               1598178122  0     0     0       14        `
[...]

But, if I remove the -x c++ flag, I don't get this error anymore, and everything builds fine. Why is this?

Anakhand
  • 2,838
  • 1
  • 22
  • 50
  • Why do you ever need `-x c++` option? Isn't a compiler automatically interprets `.cpp` as C++ source file? If you want to compile `.c` as C++, then see [that question](https://stackoverflow.com/questions/7690800/can-cmake-use-g-to-compile-c-files). – Tsyvarev Aug 23 '20 at 10:59
  • @Tsyvarev True, I don't need it at a practical level; but still it shouldn't produce an error, it's just redundant in that case. (And it would be needed if I was using a non-standard file extension.) – Anakhand Aug 23 '20 at 11:19
  • "And it would be needed if I was using a non-standard file extension." - Non standard file extensions should still be recognized by CMake. Otherwise they won't be compiled irrespective to `CMAKE_CXX_FLAGS`. As for particular error you got, try to examine **exact** command lines used for compiling and linking. While these command lines are produced by CMake, it expects `CMAKE_CXX_FLAGS` to contain "global" flags, which affect on the whole produced command line. But `-x` flag could affect only on a part of the command line, if that command line contains, e.g, `-c` flag. – Tsyvarev Aug 23 '20 at 11:29

0 Answers0