Clion IDE C++ false positive on syntax error - syntax-error

I am using Clion as IDE
I just updated my fedora from 24 to 25 in order to use C++17,
But unfortunately now, CLion (v 2017.2.3) display code error that wasn't there before the OS upgrade
Here an "error" shown using template argument as template for std::optional
Here an "error" shown using std::make_shared, saying I am using too many argument (I should need 2 apparently) while the actual constructor takes 3 argument. I remind that the code compile and works
My CMake file (dunno if it could help for this)
Cmake_minimum_required(VERSION 3.3)
project(FreeSouls CXX)
set(CMAKE_CXX_COMPILER /usr/bin/g++)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lprotobuf -lboost_timer -lboost_system -lboost_thread")
set(CMAKE_CXX_STANDARD 17)
if ( UNIX )
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -Wall -Wpedantic -Wextra -Wno-deprecated-declarations")
message( STATUS Set Warnings flag : ${CMAKE_CXX_FLAGS})
endif()
enable_testing()
add_subdirectory( Protobuff )
include_directories(
cmake-build-debug/Protobuff
FySMQ/include/queue
FySMQ/include/bus
FySMemoryManager/include
Server/include/gateway
Server/include/network
Server/include/babble
Server/include/game Utils/src)
set(SOURCE_FILES_MM)
set(SOURCE_PROTOBUF
cmake-build-debug/Protobuff/FySBabbleMessage.pb.cc
cmake-build-debug/Protobuff/FySGtwMessage.pb.cc
cmake-build-debug/Protobuff/FySLoginMessage.pb.cc
cmake-build-debug/Protobuff/FySAuthenticationResponse.pb.cc
)
set(SOURCE_FILES_MQ
FySMQ/include/queue/LockFreeQueue.hh
FySMQ/include/queue/QueueContainer.hh
FySMQ/include/queue/LockingQueue.hh
FySMQ/include/bus/BusListener.hh
FySMQ/include/bus/FysBus.hh)
set(SOURCE_FILES_SRV
Server/src/gateway/Gateway.cpp
Server/include/gateway/Gateway.hh
Server/src/gateway/Context.cpp
Server/include/gateway/Context.hh
Server/src/network/SessionManager.cpp
Server/include/network/SessionManager.hh
Server/src/babble/Babble.cpp
Server/include/babble/Babble.hh
Server/src/babble/BabbleChannel.cpp
Server/include/babble/BabbleChannel.hh
Server/src/network/TcpConnection.cpp
Server/include/network/TcpConnection.hh
Server/src/gateway/GameServerInstance.cpp
Server/include/gateway/GameServerInstance.hh
Server/src/gateway/Authenticator.cpp
Server/include/gateway/Authenticator.hh
)
set(SOURCE_FILES_UTILS Utils/src/CheckVars.hh Utils/src/TokenGenerator.cpp Utils/src/TokenGenerator.hh)
set(SOURCE_FILES_ALL
${SOURCE_PROTOBUF}
${SOURCE_FILES_UTILS}
${SOURCE_FILES_MQ}
${SOURCE_FILES_MM}
${SOURCE_FILES_SRV})
set(SOURCE_FILES
${SOURCE_FILES_ALL}
Server/src/main.cpp)
set(SOURCE_FILES_TEST
${SOURCE_FILES_ALL}
FySMQ/test/TestUnitMQ.cpp
Utils/test/TestCheckVar.cpp
FySMQ/test/FysBusTest.hh)
### Server ###
add_executable(FreeSouls ${SOURCE_FILES})
target_link_libraries(FreeSouls proto )
# link against dynamic libraries
add_definitions(-DBOOST_ALL_DYN_LINK)
## Test executable
find_package(Boost COMPONENTS unit_test_framework REQUIRED)
add_executable(UnitTests ${SOURCE_FILES_TEST})
target_link_libraries(UnitTests ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
add_test(UnitTests UnitTests)
Does this error ever happened to you? How can I fix that?
I found a post about a problem similar here
Linux CLion can't resolve namespace member
But it looked like it were a bug of the Clion and a new version fixed it,
knowing I was using the exact same version on fedora 24 and it was working fine before the upgrade, I don't know if it is the same.

As said by Chris, this was a bug from the CLion parser.
The release of the version 2017.3 has solved those issues.

Related

gtkmm 4.0 header build errors when compiling own project

I recently updated my gtkmm framework to version 4.0.0 and updated code to be compatible with the framework. I'm using msys2 in JetBrains CLion on Windows. However when I'm compiling project I got wall of warnings and errors in various framework headers like gtkmm, glibmm etc. I can't find errors which could be related to my code. Here is my Cmake configurations
cmake_minimum_required (VERSION 3.17.5)
set (CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS " -lstdc++fs")
project (Accounter)
find_package(Boost 1.73 COMPONENTS date_time REQUIRED)
FIND_LIBRARY (LIBZIP_LIBRARY NAMES zip)
FIND_PATH (LIBZIP_INCLUDE_DIR zip.h PATH_SUFFIXES include/zip include ) # Find header
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE(PkgConfig REQUIRED)
pkg_check_modules(GTKMM gtkmm-4.0)
include_directories( ${GTKMM_INCLUDE_DIRS} )
link_directories( ${GTKMM_LIBRARY_DIRS} )
#ADD_DEFINITIONS(${GTK_CFLAGS_OTHER})
FIND_PACKAGE_HANDLE_STANDARD_ARGS(libzip DEFAULT_MSG LIBZIP_LIBRARY LIBZIP_INCLUDE_DIR)
link_directories((Accounting))
add_subdirectory(Accounting)
link_directories(OdsFile)
add_subdirectory(OdsFile)
link_directories(GUI)
add_subdirectory(GUI)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(Accounter main.cpp)
target_link_libraries(Accounter LINK_PUBLIC Accounting OdsFile GUI ${Boost_LIBRARIES} ${LIBZIP_LIBRARY} stdc++fs ${GTKMM_LIBRARIES} )
else()
message( FATAL_ERROR "Boost library is required for this library")
ENDIF().
The compile log is under this link . Do I miss something in cmake configuration? How it should be compiled? I'll provide additional info, files if it will be required. I think errors should be on my side not on framework.

CMake: Not Connecting -lrt to project

I have a project, dependent on the rt library, that shows undeclared errors for functions ddefined in LIBRT when using make following running the "cmake .". I've looked at the plethora of suggestions for questions similar to this but none worked for me. Using the functions check_include_files() and check_library_exists() return TRUE, and it appears I succeed in finding the library using find_library(). I even get "-lrt" appended at the end of the link.txt file.
Anyways, I am a bit stumped and looking for some guidance. Below is my CMakeLists.txt file:
cmake_minimum_required (VERSION 3.5)
project (proj_name C)
enable_language(C)
set (SOURCES
/source/file/path
)
add_definitions (-std=c99)
add_executable (proj_name ${SOURCES})
find_library(LIBRT rt)
if(LIBRT)
target_include_directories (proj_name PUBLIC ${PROJECT_SOURCE_DIR}
/proj/includes/path #not including rt header path
${RT_INCLUDE_DIRS}
)
target_link_libraries(proj_name PUBLIC ${LIBRT})
endif(LIBRT)
set(CMAKE_C_COMPILER /usr/bin/gcc)
include(CheckIncludeFiles)
check_include_files(mqueue.h HAVE_MQUEUE_H)
if(NOT HAVE_MQUEUE_H)
message(FATAL_ERROR "mqueue.h not found")
endif(NOT HAVE_MQUEUE_H)
include(CheckLibraryExists)
check_library_exists(rt timer_gettime "" HAVE_RT)
if(NOT HAVE_RT)
message(FATAL_ERROR "timer_gettime not found")
endif(NOT HAVE_RT)
Running on WSL Ubuntu.
Thanks for any insight possible!

MacOS, CMake and OpenMP

I am using the newest CMake (3.9.3) from Homebrew along with LLVM 5.0.0 also from Brew, because Clang here has OpenMP support.
This worked in CMake 3.8.2 with LLVM 5.
In my CMakeLists.txt I have
find_package( OpenMP )
and later I want to do
if( OpenMP_CXX_FOUND )
However CMake doesn't seem to pick up on the find_package directive.
I run CMake with
cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang -DUSE_WERROR=ON
where I have checked that clang and clang++ points correctly to /usr/local/opt/llvm/bin/clang and /usr/local/opt/llvm/bin/clang++
All I get is these two lines:
-- Could NOT find OpenMP_C (missing: OpenMP_C_FLAGS OpenMP_C_LIB_NAMES) (found version "1.0")
-- Could NOT find OpenMP_CXX (missing: OpenMP_CXX_FLAGS OpenMP_CXX_LIB_NAMES) (found version "1.0")
If I set OpenMP_C_FLAGS myself (with -DOpenMP_C_FLAGS=-fopenmp=libomp) it changes the error to
-- Could NOT find OpenMP_C (missing: OpenMP_C_LIB_NAMES) (found version "3.1")
Notice that it changes the version number, so it must be finding something, right?
What am I missing for this to work properly?
Okay, it seem that inside the FindOpenMP.cmake supplied by CMake we do a try_compile, which fails silently (because we do it a lot of times and most of them will fail, this makes sense). However, with Clang a -Werror flag is supplied, which fails because of an unused command line argument. I can thus add:
if(APPLE)
if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
set(OpenMP_C_FLAG "-fopenmp=libomp -Wno-unused-command-line-argument")
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(OpenMP_CXX_FLAG "-fopenmp=libomp -Wno-unused-command-line-argument")
endif()
endif()
to my project because I know that -fopenmp=libomp will work for this Clang.
Is this the right way of doing it?
The message basically tells you that you have to provide the path to the libraries and the names of the libraries. The following example should fix your problem (see also find_package(OpenMP)). Note that I use the brew installation using the command "brew install llvm". The first four lines are just for completeness.
set(CMAKE_C_COMPILER "/usr/local/Cellar/llvm/5.0.1/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/local/Cellar/llvm/5.0.1/bin/clang++")
set(OPENMP_LIBRARIES "/usr/local/Cellar/llvm/5.0.1/lib")
set(OPENMP_INCLUDES "/usr/local/Cellar/llvm/5.0.1/include")
OPTION (USE_OpenMP "Use OpenMP to enamble <omp.h>" ON)
# Find OpenMP
if(APPLE AND USE_OpenMP)
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
set(OpenMP_C "${CMAKE_C_COMPILER}")
set(OpenMP_C_FLAGS "-fopenmp=libomp -Wno-unused-command-line-argument")
set(OpenMP_C_LIB_NAMES "libomp" "libgomp" "libiomp5")
set(OpenMP_libomp_LIBRARY ${OpenMP_C_LIB_NAMES})
set(OpenMP_libgomp_LIBRARY ${OpenMP_C_LIB_NAMES})
set(OpenMP_libiomp5_LIBRARY ${OpenMP_C_LIB_NAMES})
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(OpenMP_CXX "${CMAKE_CXX_COMPILER}")
set(OpenMP_CXX_FLAGS "-fopenmp=libomp -Wno-unused-command-line-argument")
set(OpenMP_CXX_LIB_NAMES "libomp" "libgomp" "libiomp5")
set(OpenMP_libomp_LIBRARY ${OpenMP_CXX_LIB_NAMES})
set(OpenMP_libgomp_LIBRARY ${OpenMP_CXX_LIB_NAMES})
set(OpenMP_libiomp5_LIBRARY ${OpenMP_CXX_LIB_NAMES})
endif()
endif()
if(USE_OpenMP)
find_package(OpenMP REQUIRED)
endif(USE_OpenMP)
if (OPENMP_FOUND)
include_directories("${OPENMP_INCLUDES}")
link_directories("${OPENMP_LIBRARIES}")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
# set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif(OPENMP_FOUND)
You might want to set e.g. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lpthread") such that the linker automatically detects the appropriate pthread library (see pthread and wiki).
Apparently, case is important. For an unrelated project I can make it work with
find_package ( OPENMP REQUIRED )
This didn't work:
find_package ( OpenMP REQUIRED )
With that directive, no need for setting all the other flags by hand.
cmake 3.13.2, clang-1000.11.45.5 (High Sierra)
Maybe it's a CMake version thing, I come up with a slightly different solution with Franzi's.
I also use brew install libomp on my machine. It seems like OpenMP_CXX_FLAGS is used for compiling project source code instead of compiling the omp (the flag is stored in the omp target and will be populated by command target_link_libraries).
Besides that, OpenMP_CXX_LIB_NAMES shouldn't have prefix lib because it will cause an error like -llibomp not found, where -lomp should be used instead.
I also noticed that CMAKE_C_COMPILER_ID is AppleClang instead of Clang if I put project(playground) after cmake_minimum_required. In reverse, it's Clang, quite annoying and I don't know why.
Xpreprocessor used here is because apple clang doesn't ship with OpenMP and this flag tells the compiler to look for pragma (preprocessor expansion) elsewhere. In our case, it's the header files in the include path where the libomp is installed.
cmake_minimum_required(VERSION 3.12)
project(playground)
if(APPLE)
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
if(CMAKE_C_COMPILER_ID MATCHES "Clang\$")
set(OpenMP_C_FLAGS "-Xpreprocessor -fopenmp")
set(OpenMP_C_LIB_NAMES "omp")
set(OpenMP_omp_LIBRARY omp)
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang\$")
set(OpenMP_CXX_FLAGS "-Xpreprocessor -fopenmp")
set(OpenMP_CXX_LIB_NAMES "omp")
set(OpenMP_omp_LIBRARY omp)
endif()
endif()
find_package(OpenMP REQUIRED)
add_executable(helloworld helloworld.cxx)
target_link_libraries(helloworld PRIVATE OpenMP::OpenMP_CXX)
Here's my helloworld
#include <cstdio>
#include <thread>
#include <sstream>
int main(void)
{
#pragma omp parallel
{
std::stringstream ss;
ss << std::this_thread::get_id();
printf("%s, Hello, world.\n", ss.str().c_str());
}
return 0;
}
output is,
0x700002dc8000, Hello, world.
0x10a17d5c0, Hello, world.
0x7000045d1000, Hello, world.
0x7000055d7000, Hello, world.
0x700005dda000, Hello, world.
0x7000035cb000, Hello, world.
0x7000065dd000, Hello, world.
0x700003dce000, Hello, world.
0x700007de6000, Hello, world.
0x700004dd4000, Hello, world.
0x7000075e3000, Hello, world.
0x700006de0000, Hello, world.
With recent versions of CMake (3.18, didn't work with 3.14) and a fresh installation of MacOS (with developer CL tools installed, of course), brew install libomp was the only action needed to make things work.
MacOS Ventura
cmake: /usr/local/Cellar/cmake/3.25.1
OpenMP: /usr/local/Cellar/libomp/15.0.7
llvm: /usr/local/Cellar/llvm/15.0.7_1
above solution worked after setting compiler directories and libomp directories to the correct values
with the change:
set(OpenMP_C_FLAGS "-fopenmp=libomp")
for both the clang and clangxx sections

Link ncurses in CLion CMake

I'm currently playing around with ncurses. Ncurses is a library I installed, NOT my own file. I already did some stuff but using an IDE is much easier so I decided to use CLion (I'm on Linux so can't use Visual Studio). I got the following CMakeLists.txt:
cmake_minimum_required(VERSION 3.6)
project(ncurses)
set(CMAKE_C_STANDARD "${CMAKE_C_FLAGS} -Wall -Werror -lpdcurses")
set(SOURCE_FILES main.cpp ncurses.h)
add_executable(ncurses ${SOURCE_FILES})
My project is called ncurses I don't know if that'd matter.
I got the following main.cpp
#include <ncurses.h>
int main() {
initscr();
printw("Hello");
refresh();
getch();
endwin();
return 0;
}
However, I get the following errors:
/opt/clion/bin/cmake/bin/cmake --build /home/josh/ClionProjects/ncurses /cmake-build-debug --target all -- -j 4
make[2]: *** No rule to make target 'CMakeFiles/ncurses.dir/build'. Stop.
make[1]: *** [CMakeFiles/Makefile2:68: CMakeFiles/ncurses.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
I don't get what the problem is. I tried -lncurses except of lpdcurses but that doesn't work either. It only gives an error when building but not in the IDEA itself.
in your CMakeLists.txt
just add :
set(CMAKE_CXX_FLAGS "-lncurses")
To tell the compiler to link a library using CMake, you should use the target_link_libraries() function.
Add this in your CMakeLists.txt:
target_link_libraries(${PROJECT_NAME} ncurses)
However, the error code that you have doesn't seem to be caused by linking. Try adding this line: set(CMAKE_CXX_STANDARD 17) before your add_executable(). Replace 17 by whatever C++ version you want. I'm pretty sure that it wont change anything but heh, worth a try. Also, don't forget to reload cmake project and reset the cache.
For me solutions above did not work. However appending the last 4 lines to the code block below worked for me.
(Linux mint 20, Clion 2020.3)
CMakeLists.txt
cmake_minimum_required(VERSION 3.17)
project(<YOUR_PROJECT>)
set(CMAKE_CXX_STANDARD 14)
find_package(Curses REQUIRED)
include_directories(${CURSES_INCLUDE_DIR})
add_executable(<YOUR_PROJECT> main.cpp)
target_link_libraries(<YOUR_PROJECT> ${CURSES_LIBRARIES})
Change <YOUR_PROJECT> to your actual project name.

Cmakelist working outside of Clion

I've wanted to use Clion for awhile but I've always had trouble with Cmake. Armed with Cygwin, I've almost gotten this stupid thing to work.
The issue is while I can compile a cmake file from within a cygwin terminal, in Clion I am told it cannot find the library I want.
Error:A required package was not found
The cmakelist.txt file
cmake_minimum_required(VERSION 3.3)
project(Test)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(PKG_CONFIG_PATH /usr/lib/pkgconfig)
set(PKG_CONFIG_EXECUTABLE /usr/bin/pkg-config.exe)
set(SOURCE_FILES main.cpp)
add_executable(Test ${SOURCE_FILES})
INCLUDE(FindPkgConfig)
pkg_check_modules(SDL2 REQUIRED "sdl2")
MESSAGE(STATUS "SDL library: " ${SDL2_LDFLAGS})
TARGET_LINK_LIBRARIES(Test ${SDL2_LDFLAGS})
I have no idea if setting the variables PKG_CONFIG_PATH and others work, but they successfully build a makefile for my use in cygwin that builds correctly.
I've deleted the cache, remade the project and everything. It just refuses to work in Clion
If I understood correctly, your cmake config is unable to find SDL library. I found it better to use find_package command instead of pkg_check_modules.
In order to find_package(SDL2) to work, there must be FindSDL2.cmake module in directory, specified by CMAKE_MODULE_PATH variable (usually, it is cmake/Modules directory inside your source tree).
FindSDL2.cmake is not a part of CMake, but you can find one online easily (check my own modules, for example: https://github.com/dragn/cmake-modules).
Refer to this doc for details: https://cmake.org/Wiki/CMake:How_To_Find_Libraries.
Put FindSDL2.cmake to cmake/Modules directory and add this to your CMakeLists.txt:
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake/Modules)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
...
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARY})
NOTE: Sadly, it appears that Leonardo has not succeeded in finding volunteers for maintaining FindSDL2.cmake in SDL community: https://cmake.org/Bug/view.php?id=14826.