1

I've had this problem for a while and finally decided to just ask it. I have a folder-worth of .cpp files I would like to include into multiple projects. I have put all the .h and .cpp files into one directory C:/MyCode/ and have added the directory to "Additional Include Directories" in Visual Studio. For the .cpp files, I know I could add all of them manually, one at a time, but I would like to use this code for multiple projects and would prefer not to spend 10 minutes including each one for every project I make (I make a lot of projects.) I read here that I could use something called a wildcard to include multiple files of the same type by accessing the project file directly with notepad or something, but it doesn't seem to work in my case as it still doesn't recognize the functions I have defined. I have also tried making a library with all the function definitions in it and including it that way, but it did not work. I prefer against this method anyway as I would like to be able to edit the code in the original directory (C:/MyCode/) and have it affect the code for all the projects that use it.

So my question is, is there a way to make Visual Studio include all the .cpp files in a folder? All that I have found does not seem to work.

Community
  • 1
  • 1
TheTrueJard
  • 471
  • 4
  • 18
  • Sounds like you're searching for the concept of a library. Visual Studio supports several kinds of library. Make your file collection a static library. – Cheers and hth. - Alf Feb 06 '16 at 02:13
  • Okay then, library it is. A while ago I looked at building static libraries [here](https://msdn.microsoft.com/en-us/library/ms235627.aspx), and it seemed to work, but the library needed to be included as a reference. I have seen libraries and used libraries that can be used simply with `#pragma comment(lib, "mylib.lib")`. How would I create a library like this? – TheTrueJard Feb 06 '16 at 02:16
  • In Visual Stu-stu-studio-oh-oh, you might as well go right to the source: [Walkthrough: Creating and Using a Static Library (C++)](https://msdn.microsoft.com/en-us/library/ms235627.aspx) – user4581301 Feb 06 '16 at 02:23
  • Okay, I'll try the static library again as I just remembered I don't think I added the library directory to properties last time. – TheTrueJard Feb 06 '16 at 02:36
  • Another solution I just thought of is that I can use the multiple files for generating the code, and write a program that takes code from multiple .cpp files and puts it all in one. Then I only have to add the single file to my multiple projects, and I can still add the directory for the header files. – TheTrueJard Feb 06 '16 at 03:27
  • @TheJared802: that's what a static library is. – Cheers and hth. - Alf Feb 06 '16 at 03:49

1 Answers1

0

So my question is, is there a way to make Visual Studio include all the .cpp files in a folder? All that I have found does not seem to work.

Im not an expert on this matter. However I also read about including files with the wildcard. The downside was that the wildcard was replaced once the solution was opened once or built once or similar. There might be other options out there, but the one I tried worked like that, so any files added later were problematic.

For me it was easy to use CMake which can be used to generate the files for different compilers and platforms, so support for more than just visual studio could then also be easily accessible.

Here is a small CMakeLists.txt file I use for some of my codes:

cmake_minimum_required (VERSION 2.6)
project(project_name)

# include all cpp and header files
file(GLOB SOURCES *.cpp *.h)

add_executable(project_name ${SOURCES})
Rochet2
  • 1,146
  • 1
  • 8
  • 13
  • I have noticed these downsides of the wildcard. Thanks for the CMake example, but I'm not sure how much that will help me now. I do not use CMake. – TheTrueJard Feb 06 '16 at 02:35