No C++ support when including C++ header-only file in C project

已回答

I'm using the following header-only C++ file in my project: https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator/blob/master/src/vk_mem_alloc.h

The code in my project (apart from this file) is in C, and CLion seems to have trouble with the C++ parts of this header file (can't find cstdlib and all other C++ includes, can't resolve class, templates, etc). 

My toolchains work correctly and I can use the header file the same way without problem in a C++ project.

Does CLion support calling C++ code from C?

0

Hi, Jeremie! Please show your CMakeLists.txt.

0
Avatar
Permanently deleted user

Here it is:

cmake_minimum_required(VERSION 3.9)
project(warlords)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

find_package(Vulkan REQUIRED)

include_directories(${Vulkan_INCLUDE_DIR})

add_executable(warlords code/main.c code/external/vk_mem_alloc.hpp)

target_link_libraries(${PROJECT_NAME} ${Vulkan_LIBRARY})
0

Jeremie, it's not possible to use C and C++ languages in one file as is, but there are some ways to mix them. Please take a look at, for example, this article. In my opinion, the most simple option is to create a C++ project (in other words, to rename your main.c into main.cpp and reload the CMake project) and wrap your C code into extern "C" {}.

0
Avatar
Permanently deleted user

Thanks, separating the header into .h/.cpp did the trick.

0

请先登录再写评论。