Search code examples
c++cvisual-studioluaconan

Building Visual Studio solution with C and C++


I'm wondering if I can add a C file to a C++ project. I use premake5 and conan to get the dependencies and build the solution but every file I've used so far is C++. Now, I need a C file that has a kdtree that I need to make new features in my engine. But I don't know if I can work with both C and C++ in one project or how to do it. This is my lua definition for the project. What can I do to add one C file to the project?

project"Particles"

  kind "ConsoleApp"
  language "C++"
  targetdir "../build/%{prj.name}/%{cfg.buildcfg}"
  includedirs { "../include", "../deps/include", "../include/engine", "../deps/include/stb", "../deps/include/imgui", "../deps/include/tests" }
  conan_config_exec()
  debugargs { _MAIN_SCRIPT_DIR .. "/examples/data" }
  files {
    "../deps/src/**",
    "../deps/include/**",
    "../src/**",
    "../include/**",
    "../tests/particles_test.cpp",
    "../assets/**",
  }
  filter "files:**.obj"
      flags { "ExcludeFromBuild" }

Solution

  • Visual Studio projects support both c++ and c files. The most important thing that immediately comes to mind at the project level is that the precompiled header system is not compatible with both at the same time. It will work with either, but if you have both c++ and c files in the same project you will need to manually set one to not use precompiled headers (or have a different precompiled header for c++ and c files).

    If you are not using PCH at all then you will not need to perform any manual override.

    And also note that you will need the usual 'extern "C"' wrapping, ensuring that the declarations match between the two parts.