A BETTER VEXcode Pro comes up: ------>AUTO re-compiling .cpp/.c source file when included header .h files(#include"xxx.h") is modified, by adding some code to makefile rules

:face_with_peeking_eye: :face_with_peeking_eye:VEXcode Pro auto re-compiling .cpp/.c source file when included header .h files(#include"xxx.h") is modified, by adding some code to makefile rules。

My code structure is as follows:

All the functional modules are in a folder and placed under /src folder. Each functional module basically contains an .h file and some .cpp files

When you modify any of your header files, all source files that reference the header file are automatically recompiled :heart_eyes: so that the changes to the header file take effect.

Implementation Mode:
:joy:(1) replace you_project_folder/src/vex/mkrules.mk with this
mkrules.mk.zip (4.3 KB)
(modified mkrules.mk and a text is inside)

:joy:(2) add some makefile rules to the file: you_project_folder/src/vex/mkrules.mk by yourself
code below:
:star_struck:Note: make sure when you copy the code to your mkrules.mk, the tab character before $(conditional_generate_dependency_files) would not be changed into white spaces by VEXcode。
so the easierway is (1)。

###### 使用方法: 复制下面所有代码(或者本文所有内容), 通过“记事本”或者vexcode打开:你的程序项目/vex/mkrules.mk,并将代码粘贴到mkrules.mk最后面
###### instructions: copy all the code below(or the whole text of this text file), with "notepad.exe" or vexcode, open the file: your-project-folder/vex/mkrules.mk, then paste  the code to the end of mkrules.mk 
###########################################################################################################
### 生成自动依赖关系的文件,当头文件修改时,可以自动重新编译关联的cpp和c文件
### Generates an automatic dependency .d file, 
### which automatically recompiles the associated .cpp and .c file when the header .h file is modified!
### 以下是windows CMD代, linux 好像不一样
### the code works in windows CMD, maybe not in linux

### 命令包1: 生成 .d 文件的命令包,供cpp和c文件使用。如果头文件不存在,则只会生成空文件
### command pack 1: generate .d file for compinling c\cpp files
define gen_dep_with_headers_check_except_trailing_space
$(Q)$(MKDIR)
@$(ECHO) -------------making dependency file^: $@ , added by LQ from VEX team 74000
@$(CXX) $(INC) -MM -MT "$@ $(subst .d,.o,$@)"  $< >$@ 2>nul
endef

### 命令包2: 判断vexcode要做的事情:清除 或者 编译
### 如果传递的指令是clean等,则不执行;编译时,MAKECMDGOALS为空,则执行gen_dep来生成d文件
### command pack 2: if make clean, it's not going to generate .d files;  
### if compiling project(only "make" called,thus $(MAKECMDGOALS) is empty),  $(gen_dep) will run to generate .d files
define conditional_generate_dependency_files
$(if  $(MAKECMDGOALS), , $(gen_dep_with_headers_check_except_trailing_space))
endef

### c\cpp文件的d文件静态生成模式
### Static Pattern Rule for .d file from cpp and c file
$(BUILD)/%.d: %.cpp	$(SRC_H) $(SRC_A)
	$(conditional_generate_dependency_files)
$(BUILD)/%.d: %.c $(SRC_H) $(SRC_A)
	$(conditional_generate_dependency_files)

### 引入所有的.d依赖文件
### include all .d files for auto recompiling
-include $(OBJ:.o=.d)
###########################################################################################################

###### 注意 1:粘贴后,如果指令($(conditional_generate_dependency_files) )前的tab不小心被vexcode替换成了两个空格,则会出现类似报错:vex/mkrules.mk:64: *** multiple target patterns.  Stop.  
###### 解决办法:重新粘贴,不要在vexcode中编辑。
###### Note 1: After pasting, if the tab in front of the command($(conditional_generate_dependency_files) ) is replaced by Vexcode with two spaces, a similar error occurs: Vex mkrules.mk : 64: * * * multiple target patterns. Stop.
###### Solution: just re-paste, save , and do not edit mkrules.mk in vexcode

###### 注意 2:在cpp/c 的源代码中,#include指令的<> 或者"" 中的头文件名字后面,不要加入空格,例如:#include "my_header.h   "。否则d文件能成功生成,但是会报错。
###### 解决办法:删除头文件名后的多余空格,然后关闭vexcode,重新双击你的项目文件(xxx.v5code),软件就会自动清除编译的缓存; 或者你自己手动删除项目文件夹中的build文件夹。
###### Note 2: in the CPP/C source code, do not add spaces after the name of the header file in the #include directive, for example: # include "my_header.h   ". Otherwise, the .d file can be generated successfully, 
###### but an error will be reported. 
###### Solution: remove the extra space after the header file name, close vexcode, and double-click Your Project File (xxx.v5code) , the software automatically clears the compiled cache; or you can manually delete the build folder from the project folder.

PS: I am using Windows, the code need to be modified for linux。

what do you mean by this?

As I am not super familiar with the process of ‘compiling’ / ‘building’ (preprocessor, compiling, linking etc.) could you explain how this differs from the default behavior of VEXcode Pro?

I was under the impression that VEXcode Pro already did this. (It seems like I was wrong though, judging by this post)

The main reason I am confused is that it seems like VEXcode Pro MUST do this otherwise it wouldn’t function properly in the first place.

Nevertheless, this seems pretty impressive. :+1:

by default,if all your .h files is in include/ folder,and your cpp file #include it,when you edit the .h file,ALL your cpp files will be re-compiled。

But if your .h files is in “src/” folder, or one level lower folder like “src/lower_folder/” , and only #included by some of your cpp files, if you edit the .h files, and click download button( or Ctrl+B), nothing will happen, except the brain will run the old program, that means the .h file editing is not updated to the related cpp files。

With the extra makefile rules in my post added, when you edit your .h file(s), no matter where they are, Build button( or Ctrl+B) will cause all the related cpp files ( namely cpp files that #include directly or relatively the edited .h file(s) ) to RE-COMPILE and the non-related cpp files will stay UN-CHANGED。

PS: VEXcode only supports one level lower folder by default。 you can change this though。

here relatively #include means nested #include, like a.cpp #include a.h ,and a.h #include b.h,thus a.cpp relatively #include b.h。

They probably meant that when you modify only a header file it doesn’t realize you changed stuff when you try to compile. This has happened to my team a couple times.

the extra makefile rules mainly generates .d files to tell the compiler which .h files the cpp source files depend on (#include )。 when .h files is modified,the related cpp files is then recompiled。

yes,exactly!! by default,you can edit vex.h to make your changes to update, but all the cpp files will be recompiled。

This is inadvisable. Headers should go in a headers or include directory tree and source files should go in a separate src directory tree.

6 Likes

but you can not separate your code according to what they do 。i intend to separate them into different folders by functioning, like a moudle。

and if you edit any one of your header file in include/ folder,all your cpp files will be recompiled 。 this is set by default。 and i did not delete this。

My team usually sets up our files like this so we can have groups of code like what you are saying:

include
    gui
        gui.h
        button.h
    robot
        drivetrain.h
        intake.h
        flywheel.h
    vex.h
src
    gui
        gui.cpp
        button.cpp
    robot
        drivetrain.cpp
        intake.cpp
        flywheel.cpp
    main.cpp

This way we can still have our cpp and hpp files in the src and include folders

1 Like

Damn, Chinese mentors developing private libraries for their teams to use? This is absolutely legal and fair!

image

6 Likes

Ah, I see now.image

yes, Your code structure is also very clear, :smiling_face:

:kissing:The difference is that, by default, Vexcode won‘t compile any cpp files in your codes when you change the header file in the lower folder of include/, such as include/robot/drive.h. And when you change the h file in include/, as in VEX.h, VEXcode recompiles ALL cpp files.
:hugs:However, if additional makefile rules that I write is added to vex\mkrules.mk , when you change the header file like include/robot/drive.h, src/robot/drive.cpp will be RE-COMPILED,and recompiling will also be done to src/main.cpp , because main.cpp somehow( directly or indirectly ) #include include/robot/drive.h , :joy:unless your main.cpp does not concern “drive” moudle.

This is the code of your code structure, which has the default makefile rules, namyly the original vex\mkrules.mk
iseau395-origial.zip (78.8 KB)

This is exactly the same code, but I added additional makefile rules to the end of vex\mkrules.mk 。
iseau395-extra-makefile_rules-added.zip (84.7 KB)

YOU CAN TRY IT OUT! regards.

yes, I teach them programming, and they use the same library。every year I can teach new students how the library functions is coded, and how to make the structrue of codes and they can use these library functions, such as PID, virtual button, and then adjust the parameters based on their robots, and write usercontrl and automous in specified file.

but they have different motor\sensor PORT setting in differert _IO_set__xx.h files, in which motor\sensor instance is defined,like: motor mtr_LF(PORT12, gearSetting::ratio18_1, true);
image

and one final io_choosing cpp file __IO_ALL.cpp conditional #include the _IO_set__xx.h file, accoording to the “varible” value of R_ROBOT, resulting one and only motor\sensor definition in _IO_set__xx.h is included and compiled.
image

seems like that may be close or perhaps over the line when it comes to the student centered policy.

13 Likes

i assume it is a pattern or library that can be imported if needed like “stdio.h”, if it not legitimate
,i would not give the library source files。

@jpearman By the way, well-konwn jpearman, can you consider the extra makefile rules above added into mkrules.mk,it’s more intelligent as many IDE supports。

can you consider the extra makefile rules above added into mkrules.mk,it’s more intelligent as many IDE supports。

I haven’t had a chance to look at what you did yet, but makefiles can certainly get far more complex than those we include with VEXcode. Outside of VEXcode, I only tend to use makefiles directly for straightforward projects and use CMake for most everything else.

I think it’s unlikely we will change the build system in VEXcode any time soon, the most likely place we would roll out improvements would be the visual studio code extension where mkenv.mk has already received some minor updates for compatibility with newer versions of make.

5 Likes

@jpearman you can try the extra makefile file project to find what i did :smile::smile:
and i am glad to try vs code with vex extension,for VEXcode editing is not that good,eg. all files finding ,jumping back after goto definition is not supported。
thx for reply!