How can I help Intellisense find include paths based on CMake variables? - c++

I'm trying to use Android NDK in my project, for which I'm using Visual Studio Code. This means I have a CMakeLists.txt file that uses a ANDROID_NDK variable in order to find both include and source files, and building works. However, Intellisense is not working, meaning that coding is a real pain since it can't recognize includes. I tried using CMAKE_EXPORT_COMPILE_COMMANDS but it just won't work as intended. Should I just manually edit vscode's includePath and expect everyone I share my code with to do the same? Whatever the solution may be, I'd rather have CMakeLists.txt modified so it can help any IDE/editor, but vscode-specific solutions are ok if there's no other way.
I tried set(CMAKE_EXPORT_COMPILE_COMMANDS ON) and editing the appropriate vscode variable for compile commands path but it just won't help.

Related

VSCode Intellisense "cannot open source file ...... " error C++

I am a new to programming in C++, it has been 1 month since I started. I use VSCode has my main IDE and made many projects with it. But each time I move a project in a different folder or pull one from a Github repository, the intellisense tells me that it cannot open source files in every header file or source file.
example of highlighting issue
Compiling is fine, the issue is I cannot see any basic writing mistakes and correct them directly.
(Using Windows 10, Mingw32, Makefile or CMake to compile)
I tried to add the path of library include folders in the c_cpp_properties.json (as seen in many tutorials) but it doesn't work. I also tried to change the intellisense mode in the settings but with no results.
Include path example
Several solutions from other forums have been tried but nothing works.
Do you have any idea how I can solve this ?

Visual Studio Cross-Platform CMake - Includes aren't picked up, intellisense is upset about it

I have a C++ project set up something like this:
Linux box:
/home/me/.vs/SomeBigProjectDir
/opt/DependencyOne
/opt/DependencyTwo
Windows:
Local copy of the source
I've set up the cross-platform connection in Visual Studio, and am able to trigger builds on the remote host no problem. However, Intellisense gets upset about #includes - it claims that it can't find any of the included files specified.
Visual Studio has been told to invoke CMake with -DDependencyOne=/opt/DependencyOne and -DDependencyTwo=/opt/DependencyTwo. It's a bit yucky, but we're halfway through migrating off of autotools (these were previously in a configure script), and I don't see how they'd interfere with this. Having these more cleanly packed into some CMake file is an upcoming task.
Sample CMakeLists.txt:
CMAKE_MINIMUM_REQUIRED(3.14)
PROJECT(MyProject)
include_directories(${CMAKE_SOURCE_DIR})
if (DependencyOne)
include_directories(${DependencyOne}/include)
endif()
if (DependencyTwo)
include_directories(${DependencyTwo}/include)
endif()
#blah
add_subdirectory(SubDirOne)
add_subdirectory(SubDirTwo)
#etc
#various targets are declared in subdirectories
The project itself contains many targets, most of which has a dependency on DependencyOne.
If I run a build, Visual Studio happily rsyncs across any file changes, triggers the build on the remote, and it completes without issue. In addition, it has copied back all of the includes to local (on Windows), where they live in \AppData\Local\Microsoft\Linux\HeaderCache\1.0<some numbers>\opt\DependencyOne, for example. So, it clearly knows about them as includes.
Intellisense does not seem to want to believe that these files exist, and I can not find where to tell it to look to find them. Having red squiggles on tons of code that uses these dependencies is just unhelpful, and there is no way to F12 into the header file.
I've tried adding the remote include directories to the remoteCopyAdditionalIncludeDirectories within CMakeSettings.json on Visual Studio to no avail.
I've also tried looking around the internet, but Microsoft helpfully called Visual Studio and Visual Studio Code very similar names, and it's tough to find results specifically for Visual Studio that relate to this type of problem.
Other maybe-relevant information:
Visual Studio 2019 16.9.2
CMake 3.14.6
Red Hat Enterprise Linux 7
Edit:
I re-read through this and thought I could add a little more detail.
To my understanding, in CMake, using include_directories will result in compilers being happy with the following:
include_directories(/opt/dependency/include)
for:
opt
-dependency
--include
---Dep
----SomeHeader.h
----AnotherHeader.h
#include <Dep/SomeHeader.h> //works anywhere that the include_directories() statement applied
In this case, there is that above-mentioned directory living in AppData. The root of the issue is that intellisense doesn't have the sense to look in that directory, despite the project "knowing" that includes are there (it copied the headers over). Because this is a CMake project, there is no CppProperties.json to edit to manually add the paths, at least, not one that I have found.
Edit:
This appears to be a repeat of a bug reported here: https://developercommunity.visualstudio.com/t/Intellisense-in-CMake-setup-does-not-rea/957818. I was quick to judge all headers as causing intellisense to fail - it is only those that have a '.' character in their path. I have reported this to Microsoft.

How to specify source directory location in CMakeLists.txt?

In my c++ cmake project I want my CMakeLists.txt and source files in different locations. How can you specify this to cmake? I want to know this because I believe it makes sense to separate build files from source files.
In my case, this is for a cross platform project. I know one usually uses a single top-level CMakeLists.txt + conditional constructs to handle different targets/platforms, but what I plan is to have different CMakeLists.txt files for each platform and have them AWAY from the sources. Like so:
-my_project
-sources
-module1
-common.hpp
-common.cpp
-windows
-win_functions.cpp
-win_functions.hpp
-linux
-linux_functions.cpp
-linux_functions.hpp
-module2 (...)
-module3 (...)
-build_projects
-windows
-CMakeLists.txt
-linux
-CMakeLists.txt
It seems to me it would be a matter of specifying a new cmake's working/source directory but so far the closet I get is to prepend the correct location when adding source files as in:
target_sources(my_target_windows PRIVATE ${win_source_dir}/win_functions.cpp)
But the problem is visual studio won't let me preview the source files when adding them in this way, and it seems I cannot solve this in visual studio's CMakeSettings.json either. So I am looking for a more built-in feature for this.
Ok, found a fix for my problem within Visual Studio:
In solution explorer set "solutions and folders" to "CMake Targets View" and it will now correctly display sources and headers added to the cmake project.
So, using relative paths to sources in cmake + this VS feature solves my problem.

How to include SFML source code into my visual studio 2017 c++ project and compile

I have been programming a game in c++ using the sfml library. However, I would like to adjust some of the code of that library, and use that altered code in my project.
So instead of linking the dll I would like to add the source code and then play with that source code. (e.g. for speed optimization).
I know that doing something like that is generally speaking a bad idea. Howeover, I want to learn by playing around a bit and trying different things.
So how would I add the sfml source code to my c++ project in MS visual studio. Note that I am a total noob. I already tried adding the sfml folder that I downloaded from git in the project properties page called "Additional Include Directories", but i am getting errors, of the form "Cannot open include file: 'SFML/Graphics/GLCheck.hpp': No such file or directory" so I guess that i have not yet done enough.
If you want to modify the source code in the library, all you'd have to do is just navigate to where you have SFML installed and go into the code files with a text editor and edit them.
Then, you could link the library to your VS project the same way you would normally but that library is now modified by you.
Seeing as you have a search directory issue already in VS, you must fix that first. Fix that and then go and modify the library's .hpp, .h, .cpp, whatever files in-place.
To fix the search issue.... I don't use VS for graphics, I use CodeBlocks so I am not sure about their GUI to link libraries and change search directories... but, find out where you installed SFML. Check your /usr/include/, it's probably there. Specify that path in the search directories. Just go and find where that GLCheck.hpp file is located. For Example: Say it's full path is /usr/include/SFML/Graphics/GLCheck.hpp... then /usr/include/SFML/Graphics/ or just /usr/include/ (VS might handle it recursively) needs to be in the list of SEARCH DIRECTORIES.

Refactring in CLion isn't working

I have problem with refactoring, e.g. renaming classes in CLion. CLion shows me the refactoring windows, but doesn't find anything to refactor. Screenshots are appended. I also tried to add header files into CMakeLists, but without success. Do you have any idea what I should do?
Thank you.
Edit: I am using Debian 8.2 x64 with Cinnamon
I just added all .h files into CMakeLists.txt into set(SOURCE_FILES ....) and now its work fine.
Thanks to V-R.
If renaming doesn't work, it could be because your project does not successfully build. Try it with a simplest project, e.g. create a completely new Hello World program. I'm sure if it builds successfully, refactoring will work as well. Worked for me in CLion 1.2.4 on OS X 10.9.4.
Concerning CMakeLists.txt, it is a standard file that is created automatically by CLion if you create a new project. If you have an existing project, you will find CMakeLists.txt if you open the menu View->Tool Windows->Project.
My advice is to experiment by closing your existing project (File->Close Project), creating a New Project and playing around with the CLion features. Here is a nice Quick Start Guide for CLion