Link ncurses in CLion CMake - 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.

Related

build yaml-cpp lib with -m32 (32-bit) - on linux /w gcc or llvm

subject pretty much says it all:
I downloaded yaml-cpp version 0.6.3.
I need to compile on linux x86_64, target linux x86_32 (build on 64 bit, use result on 32-bit)
I have been trying to add a new "YAML_BUILD_32BIT" option - similar to the existing YAML_BUILD_SHARED_LIBS option.
When I detect YAML_BUILD_32BIT is set: I try to add "-m32" to a bunch of cmake variables.
My problem is that this list of variables seems endless or not well defined.
"yaml_cxx_flags" are passed to the compile and link steps for the yaml-cpp library code...but not to build the google 'mock' code. Similarly, I found other variables that I can also set, so that google-mock is compiled with -m32 as well...but then the yaml-cpp mock tests do not see the flag...and so on and so on.
I think I am missing something very fundamental. I expect that there will be a single variable I need to update...maybe 2 or 3. I don't expect to keep finding more and more.
--
Adding more specifics:
To CMakeLists.txt:
added line (immediately after the similar line which creates the YAML_BUILD_SHARED_LIBS flag)
option(YAML_BUILD_32BIT "Build with '-m32'" OFF)
then a bit later (immediately after the YAML_BUILD_SHARED_LIBS if/else):
if(YAML_BUILD_32BIT)
# seem to need this one for the shared lib link of yaml-cpp lib
# CXX_FLAGS passed to both compile and link
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
# seem to need this one, to get flag passed to gmock build
set(FLAG_TESTED "${FLAG_TESTED} -m32")
# this one passed to compile and link of testcase
set(yaml_cxx_flags "${yaml_cxx_flags} -m32")
endif()
and made "FLAG_TESTED" addive, on immediately following line:
set(FLAG_TESTED "-Wextra -Wshadow -Weffc++ -pedantic -pedantic-errors ${FLAG_TESTED}")
Given the above, then configuring with:
# using cmake/3.19.3
cmake -G "Unix Makefiles" -DYAML_BUILD_SHARED_LIBS=ON -DYAML_BUILD_32BIT=ON"
... and then building with 'make VERBOSE=1', I see that 'gmock-all.cc.o' did not receive the -m32 flag. (gmock-all.cc.o is only the first such file in my log..there are others.)
If I remove other of the lines in my CMakeLists.txt which attempted to add flags - then other compile commands or other link commands don't see -m32 and will fail.
As I said: I think there is something fundamental that I have misunderstood. I suspect that it is much easier to configure a 32-bit build than I am making it.
With some help from a coworker, I ended up doing the following:
top-level CMakeLists.txt file (near line 28, immediately following definition of YAML_BUILD_SHARED_LIBS variable):
option(YAML_BUILD_32BIT "Build with '-m32'" OFF)
if(YAML_BUILD_32BIT)
add_compile_options(-m32)
add_link_options(-m32)
endif()
in .../test/CMakeLists.txt (near line 10):
if(YAML_BUILD_32BIT)
set(GTEST_EXTRA_FLAGS "-DCMAKE_CXX_FLAGS=-m32")
endif()
then add new flag to "ExternalProject_Add(..." call (near line .../test/CMakeLists.txt:22):
ExternalProject_Add(
googletest_project
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.8.0"
INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/prefix"
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
-DBUILD_GMOCK=ON
-Dgtest_force_shared_crt=ON
${GTEST_EXTRA_FLAGS} # <- this line added
)
The above has the effect of passing the extra "-m32" flag the embedded gmocktest project.
Given the above changes, the cmake command line above generates something that will build successfully (at least on RHEL-7, with gcc/5.2.0)
Hope this can help somebody else.
Henry

Clion IDE C++ false positive on 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.

How to use CMake for Fortran and C++?

I am new to CMake and it seems really hard to get my script work. My codes can be compiled the usual way, but I really need to use CMake. I compiled with the following:
g++ vectc.cpp -c -std=c++11
gfortran vectf.f vectc.o -lstdc++
This CMakeLists.txt, that doesn't work for me:
cmake_minimum_required (VERSION 2.6)
project (add_vectors CXX Fortran)
enable_language(Fortran)
set(CMAKE_CXX_FLAGS "-c -std=c++11")
set(CMAKE_Fortran_FLAGS "CMakeFiles/executable/vectc.o -lstdc++")
add_executable( executable
vectc.cpp
vectf.f)
If I run make after cmake i get the following, and I really dont know what to do:
[ 33%] Linking CXX executable executable
c++: warning: CMakeFiles/executable.dir/vectf.f.o: linker input file unused because linking not done
c++: warning: CMakeFiles/executable.dir/vectc.cpp.o: linker input file unused because linking not done
[100%] Built target executable
Does anyone can help me with it?
Edit:
The comments shows, I did not asked well. I am pretty new to the Cmake, and I don't know why I got the warnings. Also I have not found my "executable" file.
Reformulating my previous comment as an answer:
In case of mixed language sources (CXX, Fortran) the CXX linker is used by CMake because its linker preference is higher than that of the Fortran linker. But because of the PROGRAM statement in the fortran source the Fortran Linker is needed. Setting the LINKER_LANGUAGE property by set_property(TARGET executable PROPERTY LINKER_LANGUAGE Fortran) gives CMake a hint to select the correct linker.

CMake complains about debug library not found when building release

I'm waiting for the debug version of the library from an external source, they have delivered the release version already.
We use a Find... module to locate the library. This now results in something like:
optimized;libfoo.a;debug;foo-NOTFOUND
The CMakeLists.txt file:
...
add_executable(main main.c)
target_link_libraries(main ${foo})
Initiating the build with:
cmake source/dir -DCMAKE_BUILD_TYPE=Release
But cmake still complains about the debug library missing.
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
foo
linked by target "main" in directory source/dir
Is this the expected behaviour? Can I avoid this problem without changing our Find module or force setting the foo variable before each use?
I've given it a try and you can't suppress this error. Looking at the responsible source code cmGlobalGenerator::CheckTargetProperties() this check is only skipped with INTERFACE link libraries (which you obviously don't want since it would not link anything to main).
But you can declare a placeholder IMPORTED library of the name causing the error like:
add_library(foo-NOTFOUND STATIC IMPORTED)
To reproduce your problem and test the fix I've setup the following CMakeLists.txt:
cmake_minimum_required(VERSION 3.3)
project(FooNotFound)
cmake_policy(SET CMP0057 NEW)
set(foo "optimized;libfoo.a;debug;foo-NOTFOUND")
file(WRITE main.c "int main(void) { return 0; }")
if ("foo-NOTFOUND" IN_LIST foo)
add_library(foo-NOTFOUND STATIC IMPORTED)
endif()
add_executable(main main.c)
target_link_libraries(main INTERFACE ${foo})

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.