Clion how to add files to a project - cmake

This seems really basic. How can i add files to a project without having to manually edit the CMakeLists.txt.
For example source files in another directory

CLion parses the CMakeLists.txt and uses it to generate a project view, but I believe the only way to add files to the project is to edit the CMakeLists.txt to include those files. I expect that eventually this will change similar to the way IntelliJ integrates with a pom.xml file in a Java project, but for now you edit the CMakeLists.txt.

There is also a way to make CLion to add any cpp and h files (I don't know why don't they do it by default) and is to add this line:
file(GLOB SOURCES
*.h
*.cpp
)
and also edit the line of:
add_executable(ClionProject ${SOURCE_FILES} ${SOURCES})
In this example: ClionProject is actually the name of the project. SOURCES_FILES and SOURCES can be whatever you want.
Another good idea is to go to File -> Settings -> Build, Execution, Deployment -> CMake and tick on "Automatic reload CMake project on editing"
Here is a good starting tutorial: https://www.jetbrains.com/help/clion/2016.3/quick-cmake-tutorial.html

No other option. You have to edit the CMakeLists.txt. CLion is completely cmake based IDE. Even if you need to link external libraries, you need to edit the above-mentioned file. It doesn't work like in GUI based code-blocks for example.

How about right clicking the CMakeLists.txt editor tab, clicking "Open in -> terminal", typing ls or find or whatnot, and copy-and-pasting from there?

Related

How to completely exclude a folder from compiling in CLion

I want to basically have a "trash" folder in one of my CLion projects which I can just use to dump all .cpp or whatever files that I do not want to compile. So the files that I do want to compile will be in a "main" folder and if I feel like adding a new file, I can just copy it from the "trash" folder.
This can be done in Eclipse by simply making a new "empty project" but CLion doesn't seem to have that option.
Maybe some CMake code to exclude a directory since CLion primarily relies on CMake for project building?
I have found a solution to this. I simply added the following code to CMakeLists.txt
file(GLOB Sourcefiles Source/*.cpp)
add_executable(project_name ${Sourcefiles})
where "Source" is a folder at root level of the project folder; it will hold all the main files and "project_name" is simply the name of the project.
This way CMake just looks through that folder for files that are meant to be compiled and ignores everything else. This works great in CLion where I can use the "Reload CMake Project" option to tell CMake about new files added.

What is the impact of not overwriting CMakeLists when importing code in Clion?

We have a large project already using CMake.
When importing it in CLion, it asks whether to overwrite the CMakeLists.txt. We don't want to annoy people who don't use the same IDE, thus we select "Open project".
Has it any impact on the features available in Clion ?
No, it has no impact.
Actually, if you were to "import" your project in CLion, the overwritten CMakeLists.txt would be almost useless, since it would contain just a list of all source files and one executable, as you could easily verify yourself.
"Import" is a CLion feature just to get started when a project is not under cmake already; if on the other hand a CMakeLists.txt is already there, just "Open" the project.

CMake for Code::Blocks -- how to NOT get a Makefile

Here is my setup:
Windows 7 x64, MingW, Msys, CMake, Freescale Kinetis SDK, Code::Blocks
I'm trying to get the project settings established by CMake into a proper Code::Blocks project. When I modify the provided build_debug.bat file with -G "CodeBlocks - Unix Makefiles", it indeed produces a .cbp file, as well as the normal Makefile (and it builds the project). However when I open this .cbp file in Code::Blocks, it basically just points to the Makefile, and building the project just runs make on the Makefile.
If I deselect "This is a custom Makefile" from Project Options, and add a source file to the project tree like a normal IDE, it doesn't get built correctly, ie the include files, libraries, linker stuff, compile options, etc., are not imported into the project itself. It seems the project is basically just a holder for the Makefile, so there is not much benefit to this as an IDE.
Of course if I add the source file to the original CMakeLists.txt which is part of the distribution, and rerun cmake (via the build_debug.bat file), then it works fine.
So is there any way to get a "real" IDE configuration out of CMake? I'm guessing the answer is No, since a "real" IDE configuration is a static thing, and a Makefile is a general (Turing complete) program, so there is no way in general to create this automatically, although I suspect for 99% of cases you're just specifying include directories, lib files, and compiler options, so no general programmability is truly needed.
I can probably try to figure out where the deeply obscured gcc calls are getting their include files from, what libs are being linked in, and what compile options are being used, and add all that stuff manually into a native Code::Blocks project, but this seems to defeat the purpose of having this already done for me by the package providers, and gets very tedious when building for a different CPU or development board.
Thanks
"Real configuration" is a CMakeLists.txt, and you need to modify CMakeLists when you editing project configuration. Both makefiles and IDE settings generated by CMake are temporary and you should not edit them.
Some IDEs are able to manage project configuration directly in the CMakeLists.txt

How do IDEs get the project file list from cmake CMakeList.txt? (and why are LLVMs headers not visible?)

If I build LLVM with QtCreator using CMake, the header files do not show up in the Project window.
Steps to reproduce:
$ git clone http://llvm.org/git/llvm.git
Open QtCreator
Open File or Project...
Select llvm/CMakeLists.txt
Build
Go to Project window
Twiddle open include/llvm
Expected Behaviour: Show header files
Observed Behaviour: No header files
I think I've noticed this in other IDEs as well. The IDE must be infering from the CMakeList.txt what files are interesting to show up in the Project window, and for some reason the header files are not considered.
My question is, how does QtCreator (or other IDEs) form the list of files it shows in the Project window from CMakeLists.txt, and what is it about the LLVM CMakeList.txt files that mean that header files do not show up?

How to configure Eclipse CDT for cmake?

How to configure Eclipse "Helios" with plugin CDT for cmake?
cmake all
CMake Error: The source directory "D:/javaworkspace/workspace/Planner/Debug/all" does not exist.
Eclipse always wants to use 'all' option and I don't know how to stop its to not use it.
I've seen that in "Build behavior" section, in "Preference" there have been 'all' option. I erased this, but it still works wrong (this same error).
In Eclipse-CDT you do not create cmake projects but you import cmake projects.
This is what you should do:
Say the source of your CMake project named "Planner" is located in D:/javaworkspace/src/Planner
Create a folder (the folders NEED to be parallel to each other): D:/javaworkspace/build/Planner
Go to the folder D:/javaworkspace/build/Planner and run CMake using the Eclipse generator:
cmake ../../src/Planner -G"Eclipse CDT4 - Unix Makefiles"
This will generate the make files for your Planner project.
To import this in Eclipse do the following:
File -> Import -> C/C++ -> Existing code as Makefile project
and select D:/javaworkspace/build/Planner (the build output folder with the make files) as the "Existing Code location"
However, looking at your paths it seems to me that you are working on Windows. In windows CMake can generate Visual Studio projects. If you want to use CMake I suggest first creating a "hello world" project using CMake (remember, Eclipse does not create CMake projects, you have to create a CMakeLists.txt file by hand)
What worked best for me is cmake4eclipse. It's a plugin that is available via the marketplace.
Portions from the cmake4eclipse help text:
CMake for CDT requires an existing C/C++ project to work with. It
allows to use cmake as the generator for the makefiles instead of the
generator built-in to CDT. See Enabling CMake buildscript generation
for details.
To set up a new project with existing source code, please follow these
steps:
Check out your source code.
Open the new C/C++ project wizard ("File" => "New" => "C Project" or "File" => "New" => "C++ Project").
Make sure the project location
points to root directory of your checked out files
For the project
type, select Executable. You may also select Shared Library or Static
Library, it does not matter, that information comes from your
CMakeLists.txt, but CDT requires it. Do not select Makefile project
here!
Finish project creation.
Open the "Project Properties" dialog.
Select the "C/C++ Build" node and the "Builder Settings" tab and make sure Generate Makefiles
automatically is checked.
Select the "Tool Chain Editor" node and
set "CMake Make Builder" as the current builder.
If your top level
CMakeLists.txt does not reside in the root directory of your checked
out files, select the "C/C++ General" "Path and Symbols" node and the
"Source Location" tab. Then adjust the source folder to point to the
directory containing your CMakeLists.txt file. This will tell the CDT
indexer to scan the header files in your project.
Pro Tip: Add a CMakeLists.txt file in the root directory of your checked out files, if you miss the Binaries or Archives folder in the
C/C++ Projects View. Build the project. This will invoke cmake to
generate the build scripts, if necessary, and then will invoke make.
Do not try to import an Eclipse project that you created manually
using cmake -G Eclipse CDT4 - Unix Makefiles, as that will put you on
the classic Makefile project route!
The best thing about this plugin is that it can use the cmake exported compiler options to index your project.
Open the "Project Properties" dialog.
Select the "C/C++ General" node and the "Preprocessor Includes Paths, Macros etc." tab. Select "CMAKE_EXPORT_COMPILE_COMMANDS Parser" and move it to the top of the list.
Hit "OK" to close the dialog. Make sure to trigger one build now and recreate the index.
This fixed all the nasty indexer problems that were annoying me when I only used "CDT GCC Build-In Compiler Settings" and added stuff like "-std=c++14" to "Command to get the compiler specs".
Using CMAKE in Eclipse Makefile project(on win):
1) create new "Makefile Project with Existing Code"
2) modify builder settings(Project Properties->C/C++ Build->Builder Settings):
Build command: cmd /c "mkdir ${PWD} & cd /D ${PWD} && ${CMAKE} -G "Unix Makefiles" ${ProjDirPath} && make"
Build directory: ${workspace_loc:/IoT_SDK}/build/${ConfigName}
That's all!
In addition to accepted answer you should specify eclipse version.
Eclipse Luna (4.4):
cmake -G"Eclipse CDT4 - Unix Makefiles" -D_ECLIPSE_VERSION=4.4 ../../src/Planner
Eclipse Kepler (4.3):
cmake -G"Eclipse CDT4 - Unix Makefiles" -D_ECLIPSE_VERSION=4.3 ../../src/Planner
Another completely different method.
Manually create an empty Eclipse project.
Link source directory via Project Properties -> C/C++ General -> Paths and Symbols -> Source Location.
In the Debug and Release build directories create 'Make Targets' (using the 'Make Targets View'). Leave 'Make Target' field empty and set the 'command' field to cmake -G "Unix Makefiles" <path/to/the/sources>.
When you double-click on the 'Make Target' that you've created, it should trigger cmake invocation inside the Debug/Release dirs.
In 'Project Properties -> C/C++ Build' disable built-in makefile generator.
Now normal build should work. It will also pick up any changes in the CMakeLists.txt files (to the extent that CMake can).
Maybe a more detailed description: https://stackoverflow.com/a/38140914/4742108
Whatever method I have used the external includes has not been indexed.
This is because CMAKE used the "response files" which hides all includes inside those files, instead use it directly (-IPATH_TO_SOME_DIR)
To disable the "response file" I have used following commands:
SET(CMAKE_CXX_USE_RESPONSE_FILE_FOR_INCLUDES NO)
SET(CMAKE_C_USE_RESPONSE_FILE_FOR_INCLUDES NO)
To force the CMAKE to show gcc/g++ execution I have modified the make build target to
"all VERBOSE=1". Note that this is not needed when you are using cmake4eclipse.
I have eclipse in version 2018-12 (4.10.0) with CDT in version 9.6.
This is an old question, but deserves some updates.
First is that the person who made cmake4eclipse added the parsing functionality into CDT. The feature/bug was marked as "Resolved-Fixed" on 2020-08-05 (Aug 5, 2020), so it should go into the version of Eclipse due out next month. See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=559674
The other thing to point out is that you can manually trigger parsing on files:
Configuration Options / Index source and header files opened in editor