VSCode: Intellisense does not find C Headers that are in includePath - c

I have a workspace organized like this:
U:
|-- openmpi
| |-- ompi/include
| | |--mpi.h
| | |-- ...
| |-- ....
|-- myowncode
|-- example.c
|-- interface.h
|-- ....
The workspace is on a folder mounted as U: via sshfs.
interface.h and mpi.h are included in example.c and I can build just fine. However, Intellisense gives me an error for not being able to open either interface.h or mpi.h, or any other headers.
I edited my workspace includePath in the c_cpp_properties.json to the following
"includePath": [
"${workspaceFolder}/**",
"U:/myowncode",
"U:/openmpi/**"
],
Am I missing something? Or is Intellisense not able to work on sshfs mounted drives for some reason?

Related

Bitbake recipe copy local sources in sub folders

I'm leanrning yocto and bitbake and I'm confused with fews things.
When I'm developing a C project my tree files look like :
myproject
├── commons
│ ├── commons.c
│ ├── commons.h
│ └── path.h
├── config
│ ├── config.c
│ └── config.h
├── errors
│ ├── error.c
│ └── error.h
├── files
│ ├── COPYING
│ ├── ...
├── jimini
│ ├── jimini.c # source including the main()
│ └── jimini.h
├── Doxyfile
├── CMakeLists.txt
└── jimini-collector_0.3.bb
My CMakeLists.txt look like :
cmake_minimum_required(VERSION 3.0.0)
if(CMAKE_VERSION VERSION_LESS "3.7.0")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()
project(myproject VERSION 0.3.0)
find_library(LIBCONFIG config)
message(STATUS "libconfig path : ${LIBCONFIG}")
find_library(CURL curl)
message("CURL path : ${CURL}")
find_library(JSON json-c)
message("JSON path : ${JSON}")
find_library(RT rt)
message("RT path : ${RT}")
include(CTest)
enable_testing()
aux_source_directory(../commons SOURCES)
aux_source_directory(../config SOURCES)
aux_source_directory(../errors SOURCES)
aux_source_directory(../jimini SOURCES)
message(STATUS "project folder : " ${CMAKE_CURRENT_SOURCE_DIR})
message(STATUS "My sources: " ${SOURCES})
add_executable(${PROJECT_NAME} ${SOURCES})
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
set(CMAKE_BUILD_TYPE Debug)
#set(CMAKE_BUILD_TYPE RelWithDebInfo)
target_link_libraries(${PROJECT_NAME} config)
target_link_libraries(${PROJECT_NAME} m)
target_link_libraries(${PROJECT_NAME} pthread)
target_link_libraries(${PROJECT_NAME} curl)
target_link_libraries(${PROJECT_NAME} json-c)
target_link_libraries(${PROJECT_NAME} rt)
# install the executable in /usr/bin
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin)
My recipe look like :
DESCRIPTION="This is my recipe to my super package"
MAINTAINER="Me <me#me.com>"
# Define license file and file checksum verification
LICENSE = "GPLv2"
LIC_FILES_CHKSUM = "file://COPYING;md5=39bba7d2cf0ba1036f2a6e2be52fe3f0"
DEPENDS += "libconfig"
DEPENDS += "json-c"
FILESEXTRAPATHS_prepend := "${THISDIR}"
FILESEXTRAPATHS_prepend := "${THISDIR}/files:"
FILESEXTRAPATHS_prepend := "${THISDIR}/commons:"
FILESEXTRAPATHS_prepend := "${THISDIR}/config:"
FILESEXTRAPATHS_prepend := "${THISDIR}/errors:"
FILESEXTRAPATHS_prepend := "${THISDIR}/jimini:"
# include license and cmake config files
SRC_URI+="file://COPYING file://CMakeLists.txt"
# include sources files
SRC_URI+= " file://jimini.c file://commons.c file://config.c file://error.c"
SRC_URI+= " file://jimini.h file://commons.h file://path.h file://config.h file://error.h"
# where to copy sources and headers files
S="${WORKDIR}"
# define dependencies to build and package
inherit pkgconfig cmake
I'm not understanding how can I set the recipe to copy the local sources in sub folders. Bitbake copy each sources in the same folder and the code can't be compiled.
I've try to set differently the S="${WORKDIR}" but without result.
Can you give me an explanation ?
Thank you.
EDIT :
I've corrected my files and now it's working.
My CMakeLists.txt, now look like :
cmake_minimum_required(VERSION 3.0.0)
if(CMAKE_VERSION VERSION_LESS "3.7.0")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()
project(myproject VERSION 0.3.0)
find_library(LIBCONFIG config)
message(STATUS "libconfig path : ${LIBCONFIG}")
find_library(CURL curl)
message("CURL path : ${CURL}")
find_library(JSON json-c)
message("JSON path : ${JSON}")
find_library(RT rt)
message("RT path : ${RT}")
include(CTest)
enable_testing()
aux_source_directory(commons SOURCES) # removed the ../
aux_source_directory(config SOURCES) # removed the ../
aux_source_directory(errors SOURCES) # removed the ../
aux_source_directory(jimini SOURCES) # removed the ../
message(STATUS "project folder : " ${CMAKE_CURRENT_SOURCE_DIR})
message(STATUS "My sources: " ${SOURCES})
add_executable(${PROJECT_NAME} ${SOURCES})
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
set(CMAKE_BUILD_TYPE Debug)
#set(CMAKE_BUILD_TYPE RelWithDebInfo)
target_link_libraries(${PROJECT_NAME} config)
target_link_libraries(${PROJECT_NAME} m)
target_link_libraries(${PROJECT_NAME} pthread)
target_link_libraries(${PROJECT_NAME} curl)
target_link_libraries(${PROJECT_NAME} json-c)
target_link_libraries(${PROJECT_NAME} rt)
# install the executable in /usr/bin
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin)
My recipe, now look like :
DESCRIPTION="This is my recipe to my super package"
MAINTAINER="Me <me#me.com>"
# Define license file and file checksum verification
LICENSE = "GPLv2"
LIC_FILES_CHKSUM = "file://COPYING;md5=39bba7d2cf0ba1036f2a6e2be52fe3f0"
DEPENDS += "libconfig"
DEPENDS += "json-c"
SCR_URI+= "file://files/"
SCR_URI+= "file://commons/"
SCR_URI+= "file://config/"
SCR_URI+= "file://errors/"
SCR_URI+= "file://jimini/"
# include license and cmake config files
SRC_URI+="file://COPYING file://CMakeLists.txt"
# include sources files
SRC_URI+= " file://*.c"
SRC_URI+= "file://*.h"
# where to copy sources and headers files
S="${WORKDIR}"
# define dependencies to build and package
inherit pkgconfig cmake
I've also modified my tree files as suggested in the answer.
FILESEXTRAPATHS is only to add paths where to look for files declared in SRC_URI. You should just do the following:
SRC_URI += "file://files/"
SRC_URI += "file://commons/"
SRC_URI += "file://config/"
SRC_URI += "file://errors/"
SRC_URI += "file://jimini/"
# include license and cmake config files
SRC_URI+="file://COPYING file://CMakeLists.txt"
Also, your tree layout should be the following:
myproject
├── jimini-collector-0.3 # or files, or jimini-collector
│ ├──commons
| │ ├── commons.c
| │ ├── commons.h
| │ └── path.h
| ├── config
| │ ├── config.c
| │ └── config.h
| ├── errors
| │ ├── error.c
| │ └── error.h
| ├── files
| │ ├── COPYING
| │ ├── ...
| ├── jimini
| │ ├── jimini.c # source including the main()
| │ └── jimini.h
| ├── Doxyfile
| ├── CMakeLists.txt
└── jimini-collector_0.3.bb

Cmake Debug flag condition for one build with multiple projects

I need help in compiling my projects with debug flag.
I have compiled with -DCMAKE_BUILD_TYPE=Debug .. in build folder.
I am able to run the binary after the compilation. However, the Debug flag is not being turned on. I am sort of stuck at this point. Any kind soul is willing to give a kind advise or direction on this?
My build tree is as such:
project
|------ CMakeLists.txt (The main Cmake)
|------ ProjectA
| |----- src
|.c files
| |----- include
|.h files
| |----- CMakeList.txt
|------ ProjectB
| |----- src
|.c files
| |----- include
|.h files
| |----- CMakeList.txt
|
|------ build
| |----- ...
|------ bin
| |---- executables
I have set my cmake flag in the project A and B as such:
#cmake output directory
...
#compiler
...
set(CMAKE_C_FLAGS "... -DDEBUG ...")
#Linking
...
#add executables
...
My top level CMakeLists.text is such:
project(..)
...
add_subdirectory(ProjectA)
add_subdirectory(ProjectB)
In main CMakeLists.txt add below line
## Configure debug flag to enable debug log
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wall")
Generate Makefile:
cmake ..... -DCMAKE_ARCH="x86_64" -DCMAKE_BUILD_TYPE=Debug

How to write "CMakeLists.txt" for a big project with multiple subdirectories?

I am working on a simulation project: Taking an embedded C codebase from a target platform and trying to simulate it on a Host computer for debugging or single stepping into the code.
OS: Ubuntu Linux 14.04, IDE: CodeLite, Makefile Generator: Cmake. I am confused about how to write CMakeLists.txt for the project. Following is the structure of codebase (it's all written in C):
|ARQSim\
|-->ARQSim.h
|-->ARQSim.c
|-->BaseStationCode\
| |->downlink.c
| |->neoncopy.c
| |->armCore\
| | |->common\
| | | |->Bsconfig.h
| | | |->config.h
| | |->MacSource\
| | | |->lib\
| | | | |->arqCommon.h
| | | | |->OverTheAir.h
| | | |->source\
| | | | |->beacon.c
| | | | |->proxyDhcp.c
| | | | |->ARQ\
| | | | | |->arqCommon.c
| | | | | |->arqInterface.c
| | | | | |->fragmentation\
| | | | | | |->fragBookkeeping.c
| | | | | | |->fragProcessAck.c
| | | | | |->reassembly\
| | | | | | |->reasmBookkeeping.c
| | | | | | |->reasmProcessAck.c
I am totally new to Cmake. I have read up a lot of resources on CMake and threads here on StackOverflow. But I get confused every time. Few questions I have:
Do I need only one CMakeLists.txt at root directory or every directory needs a different CMakeLists.txt file?
How to add the source files recursively in CMakeLists.txt?
What are the basic commands I need to put in CMakeLists.txt for MakeFile generation?
An example based on the structure of code mentioned above would be appreciated.
Do I need only one CMakeLists.txt at root directory or every directory needs a different CMakeLists.txt file?
You would typically have one at each level of the tree where it makes sense
eg:
root/
+--- CMakeLists.txt // your root CMakeLists
+--- foo/
| +--- CMakeLists.txt // foo component's CMakeLists
| +--- foo.c
| +--- tests/
| +--- CMakeLists.txt // foo test's CMakeLists
| +--- foo_tests.c
+--- bar/
+--- CMakeLists.txt // bar component's CMakeLists
+--- bar.c
+--- bar_impl/ // no CMakeLists for this dir, it is part of bar
| +--- bar_impl.c
+--- tests/
+--- CMakeLists.txt // bar test's CMakeLists
+--- bar_tests.c
Project root CMakeLists.txt:
In your project root CMakeLists.txt you specify minimum cmake requirement, the project name, and include the subdirectories which have your various components in them
root/CMakeLists.txt:
cmake_minimum_required (VERSION 3.5)
project (my_project C)
add_subdirectory(foo)
add_subdirectory(bar)
Component CMakeLists.txt:
Then in each component subdirectory, you have another CMakeLists.txt file where you add libraries, executables etc
root/foo/CMakeLists.txt:
add_library(foo foo.c)
target_include_directories(foo PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
add_subdirectory(tests)
root/foo/tests/CMakeLists.txt:
add_executable(foo_test foo_tests.c)
target_link_libraries(foo_test foo)
You follow this structure for bar etc...
root/bar/CMakeLists.txt:
add_library(bar
bar.c
bar_impl/bar_impl.c)
target_include_directories(bar PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(bar foo)
add_subdirectory(tests)
root/bar/tests/CMakeLists.txt:
add_executable(bar_test bar_tests.c)
target_link_libraries(bar_test bar)
Generating build files:
To bootstrap your build, you point cmake at your root/CMakeLists.txt
cd root
mkdir build
cd build
cmake ..
(or use your ide's build manager to generate its build configuration)
Further reading
For details on the various functions I've used here, consult the documentation:
cmake_minimum_required
project
add_subdirectory
target_include_directories
target_link_libraries
Finally, to answer your second question:
How to add the source files recursively in CMakeLists.txt?
This is not recommended (see this discussion for further details).
It is better to explicitly list each file you want to include in your target.
Note that if you have source files in several separate directories, yet they all belong in the same logical target, then you don't need a CMakeLists.txt file for each directory - just list the subdirectory in the filename
Example:
foo/
+--- foo.c
+--- bar.c
+--- baz/
+--- baz.c
+--- bang.c
If you want a single target foo for all the above files, you would create it as follows:
add_library(foo
foo.c
bar.c
baz/baz.c
baz/bang.c)
Or if you really wanted to use a variable to store the list of SRCS
set(SRCS
foo.c
bar.c
baz/baz.c
baz/bang.c)
add_library(foo ${SRCS})
I was also looking for a more general way of adding CMakeLists.txt files to my projects. I decided to write a CMake generator (partly because I wanted to understand how CMake works):
https://github.com/Aenteas/cmake-generator
It has a couple of additional features such as creating python wrappers (SWIG).
Writing a generator that suits everyone is impossible but I hope it will give you an idea in case you want to make your customized version.

Build file (Rake or Make?) for building a few simple libraries with common options?

So, I have a set of different libraries in C and C++ that I need to build with some common options. My current "template" makefile looks something like so:
#Change this for different MCUs. Standard includes atmega328
VARIANT = ../variants/standard
HDRS=RedBot.h
OBJS=RedBot.o RedBotAccel.o RedBotMotor.o RedBotSensor.o
CPPFLAGS = -I../arduino/ -I./ -DF_CPU=16000000UL -Os -mmcu=atmega328p
CC=avr-gcc
CPP=avr-g++
AR=avr-ar
default: libredbot.a
libredbot.a: ${OBJS}
${AR} crs libredbot.a $(OBJS)
.c.o: ${HDRS}
${CC} -I ${VARIANT} ${CFLAGS} -c $*.c
.cpp.o: ${HDRS}
${CPP} -I ${VARIANT} ${CPPFLAGS} -c $*.cpp
clean:
rm -f ${OBJS} core a.out errs *.a
I place the makefile in the same folder as all the sources. However, this is ugly for a few reasons. For one, it's only a template. I have to duplicate this across about 15 libraries. And I need for the compilation options to be very easy to change across all libraries, because multiple targets is common. Currently, the best thing I can think of is making a root makefile with the options passed to each library makefile. However, I still have to keep track of all the files (the OBJS bit). And not all libraries are capable of being built on all targets.
Can someone point me either to a more comprehensive makefile, or possibly an example build file for something like Rake that could handle this?
Make one (or more) template files that you put in a common base folder, then in each project directory place a makefile which sets flags specific to the library being built, as well as listing only the source files. Then it includes the common makefile(s) templates, which contains implicit rules and variables that takes the local flags for building.
So a structure something like this:
project root
|-- makefiles
| |-- rules.mk
| |-- vars.mk
| |-- exe.mk
| `-- lib.mk
|-- libraryA
| |-- Makefile
| `-- (other sources and headers for this library)
|-- libraryB
| |-- Makefile
| `-- (other sources and headers for this library)
|-- programA
| |-- Makefile
| `-- (other sources and headers for this program)
`-- programB
|-- Makefile
`-- (other sources and headers for this program)
The rules.mk contains rules such as clean or the implicit build rules like .c.o.
The vars.mk contains global variables and uses local flag variables to set the global flag variables like CFLAGS.
The exe.mk contains rules to make an executable program.
The lib.mk contains rules to make a library.
A local makefile will then look something like this:
LOCAL_CFLAGS = <some C flags specific to just this library/executable>
# Other local flags, e.g. LOCAL_LDFLAGS, LOCAL_LIBS, etc.
TARGET = <name of target executable/library>
LOCAL_SOURCES = <list of all source files for $(TARGET)>
LOCAL_HEADERS = <list of all headers>
include ../makefiles/vars.mk
include ../makefiles/rules.mk
include ../makefiles/exe.mk # If making an executable
include ../makefiles/lib.mk # If making a library
# Note: Don't include both the above two files
The vars.mk file can look something like this
CFLAGS = <some common C flags>
CFLAGS += $(LOCAL_CFLAGS)
# All other flag variables
HEADERS = $(LOCAL_HEADERS)
CFILES = $(filter %.c,$(LOCAL_SOURCES))
CXXFILES = $(filter %.cpp,$(LOCAL_SOURCES))
OBJECTS = $(CFILES:%.c=%.o)
OBJECTS += $(CFILES:%.cpp=%.o)
I ended up writing my own little hacky thing with Ruby to get this done easily. You can draw inspiration from it here: https://github.com/Earlz/make-wiring/blob/master/build.rb
Basically, it consists of a "template" makefile that takes many environment variables. And the build.rb script just passes them off to the makefile without you having to manually specify everything on the command line. Usage thus would look like so:
./build.rb build redbot
./build.rb build arduino
or whatever, and all flags and arguments are neatly contained within build.rb, instead of being spread among dozens of makefiles or being manually specified on the command line

Read a variable from Makefile, in a Makefile

The tree of my work folder:
.
|-- work_folder1
| |-- some.c file
| |-- Makefile B
|
|-- some.c file
|-- Makefile A
My makefile A call 'all' rule in Makefile B. The 'all' rule of Makefile B makes one .o file named 'B.o' [$(NAME) variable] and copy it on work folder (cp ../). The Makefile A compile .c file and link them with the 'B.o'. But if i decide to change the name of 'B.o' in Makefile B, for example 'my_B.o', Makefile A cannot compile cause 'B.o' dosen't exist anymore.
How can I, from Makefile A, read the $(NAME) variable of Makefile B?
You can add a special .PHONY rule in your Makefile B so that it outputs the name you want.
In Makefile B
.PHONY: get_names
get_names:
#echo "B.o"
In Makefile A
B_Files:=$(shell $(MAKE) -C work_folder_B get_names)
# You can use $(B_Files) that will contain `B.o`.
Note the use of := in Makefile A so that you run make to get the names only once.
I found it ! Many thanks to Didier !
In my Makefile B:
$(NAME): my_B.o
PHONY: get_name
get_name:
#echo $(NAME)
In my Makefile A:
B_Files:=$(shell make -s -C work_folder_B get_name)

Resources