Home > Mobile >  CMake makes strange error, even after creating all new CMakeLists.txt
CMake makes strange error, even after creating all new CMakeLists.txt

Time:08-29

I was experimenting with CMake, until i stumped on this error:

CMake Error at CMakeLists.txt:34:
  Parse error.  Expected "(", got newline with text "

  ".


-- Configuring incomplete, errors occurred!

I though, ok fine, I'll fix the code at line 34, as suggested:

cmake_minimum_required(VERSION 3.10)

# set the project name
project(msv-auth VERSION 0.1)

# add the executable
add_executable(app "${CMAKE_CURRENT_SOURCE_DIR}/src/app.cpp")

# app name
get_target_property(Z_APP_NAME app NAME)

# specify compiler
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang  )

# specify the C   standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# specify C   compiler flags
set(CMAKE_CXX_FLAGS -pthread)

if(CMAKE_BUILD_TYPE MATCHES Debug)
    set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -Wall)
    set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -o\"${CMAKE_CURRENT_SOURCE_DIR}/target/debug/${Z_APP_NAME}\")
else()
    set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -O3)
    set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -o\"${CMAKE_CURRENT_SOURCE_DIR}/target/release/${Z_APP_NAME}\")
endif()

#includes
target_include_directories(app INTERFACE "/usr/local/include/oatpp-1.3/oatpp")

#libs
target_link_directories(app INTERFACE "/usr/local/oatpp-1.3.0")
target_link_libraries(app oatpp)

But I can't seem able to fix that error even after I tried multiple times. OK, I though, let's simplify things a bit, and make a completely new CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)

# set the project name
project(msv-auth VERSION 0.1)

# add the executable
add_executable(app "${CMAKE_CURRENT_SOURCE_DIR}/src/app.cpp")

# specify compiler
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang  )

# specify C   compiler flags
set(CMAKE_CXX_FLAGS -pthread)
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -Wall)
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -o\"${CMAKE_CURRENT_SOURCE_DIR}/target/debug/app\")

#includes
target_include_directories(app INTERFACE "/usr/local/include/oatpp-1.3/oatpp")

#libs
target_link_directories(app INTERFACE "/usr/local/oatpp-1.3.0")
target_link_libraries(app oatpp)

(There is only 23 lines of code in the 2nd CMakeLists.txt file)

However, running cmake .., it produced this error:

CMake Error at CMakeLists.txt:34:
  Parse error.  Expected "(", got newline with text "

  ".


-- Configuring incomplete, errors occurred!

Exactly the same error on the same exact line number! Like, how can there be an error on line 34 if there's only 23 lines on the file?!

Even creating a new folder, renaming every file, or even running a completely different CMakeLists.txt on a completely different folder for a completely different project, produced the same exact error message. Even rebooting the computer did not fix it at all. I've even tried uninstall and installing CMake again, still the same issue.

Anyone ever stumped on this kind of error? How did you fix it?

CodePudding user response:

You don't call the good CMakeLists.

============================

Sorry to say that but your CMakeLists is ugly and aggregate many bad practices.

It should be as simple as this (you could add install target, but here is a good starter):

cmake_minimum_required(VERSION 3.10)
project(msv-auth
    LANGUAGES CXX
    VERSION 0.1
)

find_package(oatpp REQUIRED)
find_package(Threads REQUIRED) # are you sure it's a direct dependency?

add_executable(app src/app.cpp)
target_link_libraries(app PRIVATE oatpp::oatpp Threads::Threads)
target_compile_features(app PRIVATE cxx_std_17)

Everything else should be injected externally (through arguments of cmake configuration, toolchain file, CMakePresets): compiler, custom flags, exact C standard you want, oatpp local install path in CMAKE_PREFIX_PATH, etc.

CodePudding user response:

OK, I fix it! How? For some reason, all I did was this: cmake . --fresh

Well, it won't work since my CMake wasn't v3.24 (mine is v3.23) and it did complain saying that was not a known argument. However I retried cmake ., and suddenly it can compile the CMakeLists.txt just fine. I guess this is a bit of a bug on CMake, that for some reason it was stuck on broken CMakeLists.txt from another test project that I did before and somehow did not look at the CMakeLists.txt of my current directory? Or is it some FreeBSD bug? I don't know TBH.

Edit: for context on why my CMake was 3.23: this is the latest version on PKG for FreeBSD. I wasn't thinking of compiling CMake from source yet.

  • Related