0

I have a project which have custom scripts which auto-generate C++ code files with nonstandard extensions (.acpp and .ah for generated source files and headers respectively). However, this code is not compiled as I get linker errors when compiling which disappear when renaming the files from .acpp to .cpp.

set(HEADER_FILES
    normal.h
    foo.ah
)


set(SOURCE_FILES
    normal.cpp
    foo.acpp
)

add_library(lib STATIC ${SOURCE_FILES} ${HEADER_FILES})

In the above case, only normal.cpp is compiled and if it calls functions defined in foo.acpp there would be linker errors.

I have attempted to use:

  • list(APPEND CMAKE_CXX_SOURCE_FILE_EXTENSIONS acpp)
  • SET_SOURCE_FILES_PROPERTIES(${SOURCE_FILES} PROPERTIES LANGUAGE CXX)
  • add_definitions("-x c++")
  • set_target_properties(lib PROPERTIES LINKER_LANGUAGE CXX)

And have looked at these related questions:

  • Someone else solved it with a bit of a hack: https://stackoverflow.com/questions/25935811/how-do-i-add-objects-with-a-custom-extension-to-a-cmake-library – arghol Dec 27 '19 at 11:22
  • I will have a look at that, but I was hoping there was a better way to do it (not having to depend on an external scripting language) – Evan La Fontaine Dec 27 '19 at 11:30
  • Thanks. You are right, just edited it to be correct, I was typing it off memory without autocomplete. Even with the correct syntax it did not work. – Evan La Fontaine Dec 27 '19 at 11:38
  • 1
    SET_SOURCE_FILES_PROPERTIES(foo.acpp PROPERTIES LANGUAGE CXX). This worked for me in the past. It adds the proper flag to compile the source as the requested language If the generator you are using does not honor this setting I would suggest filing a bug with CMake. Otherwise you just set `COMPILE_OPTIONS` property on the source file with `-x c++`. – fdk1342 Dec 27 '19 at 15:30

1 Answers1

1

Using CMake 3.22.0 set_source_files_properties(file.nonstandard_extention PROPERTIES LANGUAGE C) or LANGUAGE CXX worked to treat a file as either C or C++. Testing on MacOS AppleClang 13.0. See CMake docs here.

Connor Fuhrman
  • 781
  • 6
  • 15