Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include header AND source files folder in Visual Studio

I am using Visual Studio (2017) and I need the following.

I have a folder where a code generator puts the .h and .c files obtained from a formal model. This folder is not controlled by me, e.g. I cannot write in it, but it is updated by another team member.

By using the /I compiler options (or Additional include directories in the project properties) I managed to import all the generated header files in my VS project. What I am supposed to do is to integrate this generated code into a specific platform, this means that I have to compile both the generated code and the integration code on the target platform. The problem is, the compiler is not able to resolve the generated function definitions of the generated code as it only sees the .h files. What I got is a linking error (external symbol not resolved)

To solve the problem, I added the existing .c files manually, one by one. The obvious problems that comes with this solution are

  • manual boring work
  • when new files are generated, I need to manually import the new files

Question is: is there an option that can be set in order to specify the path of the source files without passing them one by one?

note: just copying and pasting the generated code in the VS project folder is not an acceptable solution.

Thanks

like image 630
Dario D'Avino Avatar asked Nov 07 '18 14:11

Dario D'Avino


People also ask

How do I add a header file in Visual Studio?

Place your caret on the first line of any C# or Visual Basic file. Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Select Add file header. To apply the file header to an entire project or solution, select Project or Solution under the Fix all occurrences in: option.

Do you include header in source file?

Generally, you only want to put the minimum necessary includes into a class header file, as anyone else who uses that header will be forced to #include all of them too. In larger projects, this leads towards slower builds, dependency issues, and all sorts of other nastiness.

How do I add the include path in Visual Studio?

Open the project's Property Pages dialog box. For details, see Set C++ compiler and build properties in Visual Studio. Select the Configuration Properties > C/C++ > General property page. Modify the Additional Include Directories property.


1 Answers

If you look at https://learn.microsoft.com/en-us/cpp/ide/working-with-project-properties?view=vs-2017 then you see there is a Source Directories property that has $(VC_SourcePath) as a default but (I think) to which you can add additional paths. The documentation is unclear whether that means all source files in such a path will be included for compilation.

At the bottom of the documentation it explains how to override certain project properties by providing an external properties file. It seems you can override the targets/sources using such a file. You can generate the file using a small tool that reads the filenames in those directories and adds them to the file.

You could also analyze the .vcproj file and build a small tool that wil re-write the part with your generated source directories, reading the filenames in those directories and adding them to the section in the .vcproj file.

like image 173
Paul Ogilvie Avatar answered Oct 10 '22 12:10

Paul Ogilvie