VSCode cannot find package but CLion can (CMake) - c++

I was wondering what the difference is between using cmake and (compile_commands and compile_flags) in VSCode? I'm running into an issue where even though I've got my CMake set up correctly (builds just fine on Clion), I run into issues when trying to use the exact same code in VSCode.
I've got the offending line below
#include "palisade.h"
where palisade is installed in "/usr/local/include/palisade" and my CMake explicitly looks for it and finds it
find_package(Palisade)
I've also generated the file compile_commands.json
via cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1
but I'm still running into issues of the library not being found

Probably it will helps.
In main CMakeLists.txt add line
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
and in c_cpp_properties.json
"compileCommands": "${workspaceFolder}/build/compile_commands.json"
This will allow InteliSence to use the dependencies created by cmake.

Related

C++, setting up include with Cmake

I want to use the ICU package and I tried installing with and without vcpkg, and with vcpkg I do not get a directory for where it was installed like I see in tutorials, so I had to manually find it nor did I get a Cmake toolchain paste line from installing with vcpkg. As for the Cmake file, I try following this tutorial
https://www.youtube.com/watch?v=FeBzSYiWkEU
but when I go to the configuration manager in Visual Studio I am brought to a json file with no option to change the toolchain, and when I try to go to the settings json file in Visual Studio Code instead, I instead find three different settings json files, none of which have the toolchain file option to change like with Visual Studio. I have had similar difficulties with the Cmake file when trying to install other packages for C++ and have no idea what I am missing from tutorials that seems to no matter what have the Cmakefile.txt never work for me.
I would like to just be able to use the ICU package without having to use Visual Studio and just be able to statically link the package with my .cpp file in the same folder that VS Code uses, and just g++ compile from cmd, however I am in over my head every time I try to get the CMake file to work. If all I know for sure is that I have the latest ICU package downloaded with vcpkg, how would I go about setting up the CMake file?
cmake_minimum_required(VERSION 3.24)
project(MyICUProject)
find_package(ICU REQUIRED)
add_library(MyLibUsingICU lib.cpp)
target_link_libraries(MyLibUsingICU PRIVATE ICU::i18n) # or whatever you use from icu. check https://cmake.org/cmake/help/latest/module/FindICU.html
have a manifest (vcpkg.json) along the sources
{
"name": "MyICUProject",
"description": "<description>",
"dependencies": ["icu"]
}
Invoke with (if you are in the source dir):
cmake -G "Ninja" -B "build" -S . -DCMAKE_TOOLCHAIN_FILE=<path_to_vcpkg_toolchain>/vcpkg.cmake

Cannot open source file error with vcpkg on vscode mac

I'm trying to use external libraries (specifically opencv) with C++ but I'm having some trouble. It seems like using vcpkg is the easiest way to go about this. So far I've followed these tutorials: https://www.youtube.com/watch?v=b7SdgK7Y510 and https://www.youtube.com/watch?v=iZeK3Ie5Fz0 but I keep getting these errors when I try to include a file:
cannot open source file "opencv2/core.hpp"
'opencv2/core.hpp' file not found
The quick fix provided by vscode says to install opencv through vcpkg but I've already done that. I've linked vcpkg according to the tutorials and included the path to the cmake toolchain file in my settings.json file.
Here is the change I made to my settings.json file:
{
"cmake.configureSettings": {
"CMAKE_TOOLCHAIN_FILE": "/Users/oliverpasquesi/coding/dev/vcpkg/scripts/buildsystems/vcpkg.cmake"
}
}
Here is the CMakeLists.txt file (it is the same as the one from the 2nd tutorial listed):
cmake_minimum_required(VERSION 3.0.0)
project(photoText.cpp VERSION 0.1.0)
add_executable(photoText.cpp main.cpp)
Here is the includePath portion of the c_cpp_properties.json file (including the 2nd path doesn't make any difference in the errors being thrown):
"includePath": [
"${default}",
"/Users/oliverpasquesi/coding/dev/vcpkg/installed/x64-osx/include/opencv2/**"
]
If it's useful, I'm using vscode 1.49.2 on a mac running Darwin x64 21.5.0.
I appreciate the help!
You need to reference your library in the CMakeLists.txt file as well, since that's the file CMake uses to prepare your project for a build.
A modern way to do this is to add two lines:
A find_package() line to find the libraries you are using. As you already referenced the vcpkg.cmake toolchain file, CMake should be able to find any libraries already installed by vcpkg when you add that line. See https://cmake.org/cmake/help/latest/command/find_package.html for documentation.
A target_link_libraries() line that lists libraries to link against your project. See the CMake documentation here: https://cmake.org/cmake/help/latest/command/target_link_libraries.html
As you are using the opencv library, you may want to look at the answers to this question as well: Linking Opencv in a project using cmake
Specifically, try adding these lines to your CMakeLists.txt:
find_package(OpenCV REQUIRED)
target_link_libraries(photoText.cpp ${OpenCV_LIBS})
vcpkg documentation on its CMake integration (for additional reference): https://vcpkg.io/en/docs/users/buildsystems/cmake-integration.html
Hopefully this helps!
Disclaimer: I work on the vcpkg tool.

How to generate a C++ executable file on Clion (using Ubuntu)

I'm currently learning C++ and I'm working on a small project. I was wondering how I can generate an executable of my project. I'm not sure how to do it, I'm using Ubuntu 20.04
I've googled it but I can not find any instructions on how to do it.
Well. If you are using CLion, you can just create a new project, and check the CMake file that CLion generate. Something like this:
cmake_minimum_required(VERSION 3.20)
project(Test)
set(CMAKE_CXX_STANDARD 14)
add_executable(Test main.cpp)
And in that file there was a line with a function called add_executable, in that function you set first the exe name and then the source files. And just run the project in CLion. By default CLion create a directory calle "cmake-build-debug" where the exe file are located.
If you want to add more libraries, change the binaries source directory and more. You will need learn CMake. Also you can use CMake standalone whiteout CLion, you just need install it sudo apt-get install cmake and use then in the terminal.
Expanding over #Steback's answer:
First, a clarification: an executable file is a file that can be executed by the system. It (roughly) contains assembly commands. Under Windows executable files are marked with an .exe extension. Under linux, they are usually extension-less.
To generate an executable file from C / C++ code you ("only") need a C/C++ compiler. A default / pre-installed one on Ubuntu is gcc / g++ (whereas on Windows you need to actively install one).
CLion is an IDE and (exactly like any other IDE) can run the compiler for you. IDE (stands for Integrated Development Environment) is a program which incorporates (minimally) a text/code editor, a compiler and a debugger (all of which it invokes normally via command line, just as you can do yourself).
CLion is an advanced (and excellent) IDE. In CLion, the way you specify how exactly it should invoke the compiler is via the CMake language (not to be confused with the unix tool make which chiefly only knows to run commands conditionally on file modified date).
CMake code should be placed in a file named CMakeLists.txt in the project root directory (sometimes CLion creates this file for you automatically). A minimal cmake project looks like
# Specify cmake language version to use for this file
cmake_minimum_required(VERSION 3.10)
# Specify any name for the project
project(NameYourProject)
# A name for your executable file and the code files needed to build it
add_executable(YourExecutableName source_file1.cpp somefolder/source_file2.cpp header_file.h)
Of course this is just a very minimal example. The CMake language is a powerful language to specify build processes, with cross-platform support. You can look it up / learn it someday.
When giving the "build" command to CLion it will now do two things:
use the cmake tool to generate a set of commands for gcc/g++ (this is called "cmake configure" + "cmake generate") - according to what you wrote in CMakeLists.txt
run the generated commands to hopefully build your executable.
As a beginner, you may be better off first trying to run the compiler yourself via the command line to see what it does. You can also opt for a different IDE (e.g. CodeBlocks, eclipse, Dev C++) where you specify what you need the compiler to do via a GUI and not via CMake (although CMake is arguably more convenient).
I'm using Dev C++. When I run and compile it, it automaticcally generate a .exe file. Just install it and open ur file with it and compile it. Should work good.

Netbeans project imported from existing cmake application fails to build with filesystem error on Windows

I am attempting to import a manually-created cmake project that I had been using in a different IDE into Netbeans 8.0.2 on Windows 7. Needless to say, my cmake configuration worked fine there.
Netbeans seems to import the directory fine. I imported it in "automatic" (cmake) mode. However, when I attempt to build the project, I get a rather cryptic (Java?) error message:
Makefile:76: recipe for target 'all' failed
process_begin: CreateProcess(NULL, /C/MinGW/bin/make.exe -f CMakeFiles/Makefile2 all, ...) failed.
make (e=2): The system cannot find the file specified.
Knowing very little about Java, I am not sure how to interpret this error. The first directory (/C/MinGW/bin/make.exe) stands out to me as not being in Windows-format, but I am not sure if that's incorrect. I do indeed have a file by that name, as I copied the longer-named mingw make binary so I would only need to type "make".
Presuming this is being run in the project root, and that the first directory is formatted correctly, I don't see any problem with finding these files.
My CMakeLists.txt is:
cmake_minimum_required(VERSION 2.8.4)
set(Project_Name "Test")
set(Test_VERSION_MAJOR 1)
set(Test_VERSION_MINOR 0)
project(${Project_Name})
include_directories(
"${CMAKE_CURRENT_SOURCE_DIR}/inc"
"${CMAKE_CURRENT_SOURCE_DIR}/inc/SDL"
"C:/Users/Bakaiya/Documents/ogre/OgreMain/include"
)
file(GLOB SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
link_directories(${CMAKE_CURRENT_SOURCE_DIR} ${OPENGL_LIBRARIES})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_executable(${Project_Name} ${SOURCE_FILES})
target_link_libraries(${Project_Name} SDL2main SDL2 OgreMain) #Ogre
Running the "generate makefile" command in the IDE completes without issue, but does not fix the problem. Additionally, clean fails, but "help" does work.
This is a problem within the IDE, it seems, because if I run make from the command line in the project root, it builds without issue.
Also, I fiddled with the file path mode setting under C/C++ -> Project Options, and it did nothing. Even set to absolute, what seems to be a relative path (CMakeFiles/Makefile2) is still in the failed command. I'm not sure if that option is expected to change that sort of reference or not.
What could be wrong with this imported project to cause this issue?
However, when I attempt to build the project, I get a rather cryptic (Java?) error message:
This is an error shown by netbeans to tell you, that it was unable to execute make command successfully. Usually this indicates a wrong setting of your (mingw-) tools.
Here are some points you can check:
Don't use make from mingw/bin, you have to use the one from mingw/msys/... There's a mingw make within mingw's msys folder, usually C:\<Path to MSYS>\<Version>\bin\make.exe - this bin-path must also be set in PATH environment variable! If MSys wasn't installed with your mingw installation, please install it.
Please check the tools set in Tools -> Options -> C/C++ -> Build Tools; you can test them by clicking Versions....
(If existing) Clean the CMake generated files and clean the cmake's cache. If not done yet, please use an out-of-source build as described here.
Can you build your project from terminal (without netbeans)?
The first directory (/C/MinGW/bin/make.exe) stands out to me as not being in Windows-format, but I am not sure if that's incorrect.
This is ok and intended by mingw - it uses linux / unix like paths.
Update
Which make program should I use?
Many MinGW users have a problem because they use mingw32-make.exe from
the MinGW installation. While this seems like the right choice, it
actually breaks the build. The problem is that this is a non-Posix
implementation of the Unix make program and doesn't work well at all.
In fact, thats why the MinGW people renamed it! They've also made a
FAQ entry explaining why you should not use mingw32-make.exe. Instead,
you should use the make.exe program from the MSYS package.
As of NetBeans 6.1, the Build Tools panel no longer allows a user to
select mingw32-make. If you choose a MinGW compiler collection it will
default to make in MSYS. If MSYS is not found, it will tell you no
make program has been found.
(http://wiki.netbeans.org/MinGWInCCDevelopmentPack)

CMake not generating compile_commands.json

I'm new to CMake and I'm trying to create the compile_commands.json file to use with clang, but I'm having some difficulties generating the file and I'm not sure why. I've been able to use cmake to compile the binary person that I have below, but after that was successful I've been unable to get it to output the compile commands.
I've also tried doing the -DCMAKE_EXPORT_COMPILE_COMMANDS=ON flag, but that didn't work either. So far there's been no errors, but also no output.
Here's what my CMakeLists.txt file looks like:
cmake_minimum_required (VERSION 2.6)
project (Tutorial)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_executable(person Person.cc Pirate.cc main.cc)
This ended up being an issue with using an old version of CMake. I ended up installing the newest version and it worked as expected.
According to Clang docs
"Currently CMake (since 2.8.5) supports generation of compilation databases for Unix Makefile builds (Ninja builds in the works) with the option CMAKE_EXPORT_COMPILE_COMMANDS."
I also encountered the same problem as you.
According to CMake doc
This option (CMAKE_EXPORT_COMPILE_COMMANDS) is implemented only by Makefile Generators and the Ninja. It is ignored on other generators.
Thus, there is no solution to generate compile_commands.json file when using MSVC.
I had the same problem, compile_commands.json was not generated with cmake, version 3.16.0.
It was generated when I used the Ninja generator, but not Unix Makefiles.
That discussion gave me the fix:
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # does not produce the json file
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "") # works