eclipse indexer problem with cmake project - cmake

I've created the eclipse project with cmake. I use vtk with qt. Dir structure is as follows:
parent_dir:
source - source.h, source.cpp
build - this is where the .project resides
I've fired up the eclipse with workspace dir /path/parent .
I have followed the instructions described in
http://www.cmake.org/Wiki/Eclipse_CDT4_Generator .
Everything builds fine, but navigation is not working. That is, the eclipse gives me the warning that the source.h is not indexed yet.
Furthermore, autocompletion doesn't work with qt and vtk related classes. I had checked with Project|Properties, where the qt and vtk includes are included. What am I doing wrong? I would really like to have autocompletion nd navigation in eclipse working with my project. I'm using eclipse ganymede on ubuntu 8.04 64-bit.
thx in advance

According to the Wiki, you should have your build tree outside the source tree.
This linked resource isn't created if
the build directory is a subdirectory
of the source directory because
Eclipse doesn't allow to load projects
which have linked resources pointing
to a parent directory. So we recommend
to create your build directories not
as children, but as siblings to the
source directory.
You'll need to do something like this:
mkdir /home/user/parent_dir_build
cd /home/user/parent_dir_build
cmake /home/user/parent_dir

This took me quite a while to figure out.
You should make sure that the "Build" directory is really NOT part of the project directory, which means, the most high-level directory that contains the "CMakeLists.txt".
So in my case, I have "dir/tree/project_dir" and "dir/tree/project_dir/src", then I should create the build directories "dir/tree/build_dir".
Then, I created a small script that creates both Debug and Release projects:
#!/bin/sh
mkdir -p $1_build/Release
mkdir -p $1_build/Debug
cmake -E chdir $1_build/Release cmake -G"Eclipse CDT4 - Unix Makefiles" -DCMAKE_BUILD_TYPE=Release ../../$1
cmake -E chdir $1_build/Debug cmake -G"Eclipse CDT4 - Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug ../../$1
Call the script from "dir/tree" with argument "project_dir".
Then, in Eclipse, click "File" --> "Import" --> "General" --> "Existing Projects into Workspace".
Specify the directory "dir/tree/$1_build", it will automatically recognize both projects.
Now, both Release and Debug projects are loaded into Eclipse, and you have all nice options like Code Assiste (code completion), and quick debugging by double-clicking on errors.
Note that you can add some file-filters in Eclipse to remove the CMake files from the project tree.

Related

Cross-compiling Qtquick GUI application through command line

I am Developing a GUI using QT framework; QT-Creator for front-end development and building the app. I am cross-compiling the code that will be running on RaspberryPi and host machine Ubuntu20.04. I am able to build the application through QT-Creator but I want to compile the code through terminal i.e. write CMakeLists.txt that will detect the cross-compiler and build the source-code. Because I need to integrate my GUI code into my other source-code and have to compile from Source-code makefile.
Can anyone help me in this?
Once I have created a screen and compiled through command line. I have tried to compile from terminal through command:
cmake .
to configure the cmake into the current directory. But I am getting error into this
cmake --build . --target clean
(to clean the directory and remove the object files), and
cmake --build . --target all
(to build the executable into the current directory)
Ideally, this should run and binary file should be generated into the current directory that will be running on target board after copying to the board.

How to deploy a Find*.cmake file for an Autotools library in the correct place for Yocto?

I created a new layer over an existing Yocto git for my company project.
In this layer I added a few external autotools based libraries.
A few applications need to link against this libraries and the application projects are all cmake based.
Taking one of this libraries (e.g. libcoap) I could easily find some FindCoAP.cmake to add to my library recipe.
Now if I was running on PC, it would simply be a matter of placing this FindCoAP.cmake file in cmake's ${CMAKE_ROOT}/Modules dir, but how should I, from inside a bitbake recipe (do_install hook), proceed to make my Find*.cmake modules available to anyone's dependent projects?
Should I try to get Yocto's cmake CMAKE_ROOT variable from system-information like this or is it a safer and more reliable way?
do_install_append() {
cmake --system-information | grep CMAKE_ROOT | cut -d \" -f2
install -d ${D}/$CMAKE_ROOT}/Modules
install ${S}/FindCoAP.cmake ${D}/$CMAKE_ROOT}/Modules
}
Thanks in advance.
To ship FindFoo.cmake with non-yet-cmake project
The ideal way is to update upstream project itself. So you will update your recipe and package FindFoo.cmake appropriately.
If you want to do it right now:
Add FindFoo.cmake to your layer (into the files directory next to your recipe).
Add that cmake file to SRC_URI (i.e. SRC_URI += "file://FindFoo.cmake").
Install it in do_install into the directory ${D}${datadir}/cmake/Modules/ for example.
Package it to the dev package by FILES_${PN}-dev variable (see example recipes below).
To use that cmake by other recipe
The usual way is to package .cmake files into the ${PN}-dev package. In your case, your application (which depends on the libcoap) will just set DEPENDS = "libcoap" and all the needed files (like headers, libraries and cmake file) will be copied (well, hardlinked) to the sysroot of your application.
CMake modules are packaged in various recipes for example:
libeigen
opencv
json-spirit
Your application is cmake based, so you will use inherit cmake in the recipe. Native module search path is set in cmake.bbclass.
(BTW, I do a build test of libcoap recipe from homeassistant layer and it worked, but obviously there is no cmake shipped.)

How to use CMake to construct an OpenSceneGraph project?

I have just downloaded the OpenSceneGraph source, unzip it into
"~/OpenSceneGraph-3.0.1" directory and use CMake to create an out-of-source
eclipse make project in "~/OpenSceneGraph-3.0.1-build-eclipse-cdt"
directory. When I execute "make" in
"~/OpenSceneGraph-3.0.1-build-eclipse-cdt" directory, OpenSceneGraph builds
successfully. I have not run "sudo make install" as I do not want to
install OpenSceneGraph tightly into my Ubuntu system.
Now I want to use CMake to create a project using the compiled
OpenSceneGraph libraries. I use the following codes in CMakeLists.txt :
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT( test_proj )
FIND_PACKAGE(OpenSceneGraph)
ADD_EXECUTABLE(test test.cpp )
INCLUDE_DIRECTORIES(${OPENSCENEGRAPH_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(test ${OPENSCENEGRAPH_LIBRARIES} )
But it seems that OpenSceneGraph could not be found by CMake.
Does anyone know how CMake could find the compiled OpenSceneGraph
libraries in the "~/OpenSceneGraph-3.0.1-build-eclipse-cdt" directory and
use it to create projects as if I have tightly installed OpenSceneGraph
using "sudo make install". Thanks for any suggestion.
You don't need to install OpenSceneGraph system-wide. Just choose a CMAKE_INSTALL_PREFIX that suits you (eg. ~/osg).
Using the install command makes sure that everything is correctly in place (i.e. in the correct directory structure) for FindOpenSceneGraph.cmake (the script CMake invokes when you call FIND_PACKAGE( OpenSceneGraph ) ) to find it.
Then, you should point any of OSG_DIR, OSGDIR, or OSG_ROOT as environment variable and point it to your install location, so CMake knows where to look for it.
Edit:
#Hugues: I'll try to make it a bit clearer:
Setup up OpenSceneGraph:
Get OSG source.
When running CMake for it, choose a CMAKE_INSTALL_PREFIX that suits you, eg. ~/osg if you don't want a system-wide installation in (default) /usr/local. Do it either by stating -DCMAKE_INSTALL_PREFIX=/home/hugues/osg on the command-line or by setting it using a gui tool like ccmake or cmake-gui.
Run make install to build and install OSG.
Set the environment variable OSG_DIR to whatever you pointed CMAKE_INSTALL_PREFIX. (export OSG_DIR=<whereever_you_installed_osg>)
Setup your project:
In your CMakeLists.txt, use FIND_PACKAGE( OpenSceneGraph ) (add desired optional arguments as desired).
Use the resulting variables (like ${OpenSceneGraph_LIBRARIES} in the appropriate places in your cmake file.
Run CMake for your project.
Add this below command in your CMakeLists.txt:
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
Then put the file FindOpenSceneGraph.cmake in src/cmake dir.
FindOpenSceneGraph.cmake can be found here.

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

How to make CMake find google protobuf on windows?

I am using Google Protobuf with CMake. On Linux the Protobuf library is found by:
find_package( Protobuf REQUIRED )
CMake knows where to look for the library. How though do I get this to work in Windows? Is there an environment variable I should create, such as PROTOBUF_LIB? I have looked in FindProtobuf.cmake but cannot work out what is required.
I also struggled with this. To be more clear.
On Windows (7, similar on older windows):
Start → Control Panel → System → Advanced System Settings → Environment Variables
Then either on the top panel or the bottom panel (if you want it to apply to other users do it on the bottom), create two new variables. The 1st one is
CMAKE_INCLUDE_PATH which points at the bottom of your include path (should contain a "google" folder)
CMAKE_LIBRARY_PATH which should contain "libprotobuf" "libprotobuf-lite" "liteprotoc" .lib files.
After you create the variables press OK and then re-start cmake (or clean the cache).
Newest protobuf v3 have CMake support out of the box.
You could use protobuf repository as submodule and just use
add_subdiretory("third-party/protobuf/cmake")
to get all protobuf targets.
Then you can add dependency to protobuf with
target_link_libraries(YourLibrary libprotobuf libprotobuf-lite libprotoc)
Another possible way is available. With protobuf's CMake configuration you can build and install protobuf binaries once and use them across several projects in your development:
git clone https://github.com/google/protobuf.git
mkdir protobuf\tmp
cd protobuf\tmp
cmake ..\cmake
cmake --build .
cmake --build . --target install
Then you can use find_package with hint paths like
find_package(protobuf REQUIRED
HINTS
"C:/Program Files/protobuf"
"C:/Program Files (x86)/protobuf")
if (NOT PROTOBUF_FOUND)
message("protobuf not found")
return()
endif()
Hope this helps.
Protobuf on windows calls find_library which will search your PATH and LIB variables.
I found the way to use protobuf v2 with cmake on Windows and build it with your project settings. Please, try look to cmake-external-packages project and protobuf-v2 CMakeLists which do the job.
In fact, I wrote it because ExternalProject_Add is wrong (because does stuff in build phase instead of generation phase).
This CMakeLists.txt will download protobuf from protobuf's github releases, extract, and emit cmake targets which you should add reference to with target_link_libraries.
Use git-subtree, git-submodule or just copy this repository contents to your repository subfolder.
Then add packages you want to use with add_subdiretory. For protobuf, use:
add_subdirectory(path/to/cmake-external-packages/protobuf-v2)
Protobuf's includes will be copied to path/to/cmake-external-packages/include folder. You can customize its location in your top-level CMakeLists:
set (EXTERNAL_PACKAGES_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/third-party/include
CACHE STRING "Directory for third-party include files, where include folders will be copied")
include_directories(${EXTERNAL_PACKAGES_INCLUDE_DIR})
Just reference protobuf for your executable:
add_executable(your_exe ${your_exe_sources})
target_link_libraries(your_exe libprotobuf libprotobuf-lite libprotoc)
Hope this helps.