0

I am trying to write a CMakeLists.txt for my Unity3D NativeAudioSDK spatializer plugin (written in C++). An almost identical build configuration can be found here. The equivalent Makefile for my project looks like this:

SRCS=AudioPluginUtil.cpp \
Plugin_Spatializer.cpp \
hrtftable.cpp
OBJS=$(SRCS:.cpp=*.o)
OUTPUT=libAudioPluginDemo.so
CXXFLAGS=-I. -O2 -fPIC
LDFLAGS=-shared -rdynamic -fPIC
CXX=g++

all: $(OUTPUT)
clean:
    rm -f $(OUTPUT) $(OBJS)

$(OUTPUT): $(OBJS)
    $(CXX) $(LDFLAGS) -o $(OUTPUT) $(OBJS)

.cpp.o:
    $(CXX) $(CXXFLAGS) -c $<

I want to use CMake for cross-platform development and buildfile generation. My current CMakeLists.txt works for Windows development and looks like this:

cmake_minimum_required(VERSION 3.13)
project(DomeSpatializer)

if(WIN32)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
endif(WIN32)

file(GLOB DEPENDENCIES
  "${CMAKE_CURRENT_SOURCE_DIR}/externals/nativeaudioplugins/NativeCode/AudiopluginInterface.h"
  "${CMAKE_CURRENT_SOURCE_DIR}/externals/nativeaudioplugins/NativeCode/AudioPluginUtil.h"
  "${CMAKE_CURRENT_SOURCE_DIR}/externals/nativeaudioplugins/NativeCode/AudioPluginUtil.cpp"
)

add_library(DomeSpatializer SHARED "${CMAKE_CURRENT_SOURCE_DIR}/source/spatializer.cpp" ${DEPENDENCIES})

if(UNIX)
  target_link_options(DomeSpatializer PUBLIC "-rdynamic")
endif(UNIX)

On Linux, using the Makefile, everything works fine. However, the CMakeLists.txt generates a shared object file that Unity does not recognize as a spatializer plugin. I tried setting the linker flags (-fPIC -shared -rdynamic) using target_linker_options() or various variables such as CMAKE_EXE_LINKER_FLAGS and CMAKE_SHARED_LINKER_FLAGS without success. Is there anything I am missing? How can i achieve the same correct build configuration with CMakeLists.txt? Any help is greatly appreciated at this point.


I am calling CMake in a build.sh script like this:

cmake -DCMAKE_CXX_FLAGS="-m64 -fPIC" -DCMAKE_C_FLAGS="-m64 -fPIC"


Versions of involved software:

  • Ubuntu 18.04.1 LTS / Linux 4.15.0-29-generic
  • cmake 3.14.0
  • g++ 7.3.0
  • Unity 2018.2.1f1 Personal (64bit)

My file structure looks like this:

.
│   build.sh
│   CMakeLists.txt
│   
├───externals
│   └───nativeaudioplugins            
│       └───NativeCode
│               AudioPluginInterface.h
│               AudioPluginUtil.cpp
│               AudioPluginUtil.h
│               hrtftable.cpp
│               Makefile
│               PluginList.h
│               Plugin_Spatializer.cpp
│
└───source
        spatializer.cpp

Note that ./source/spatializer.cpp and ./externals/nativeaudioplugins/NativeCode/Plugins_Spatializer.cpp are identical, but i want to use spatializer.cpp in my CMakeLists.txt.

Byomeer
  • 426
  • 6
  • 10
  • 1
    You may use `make VERBOSE=1` for CMake-generated project to see exact command lines executed. Compare options in the linker command line with one you have in hand-written Makefile. In CMake analogue of `CXXFLAGS` is `CMAKE_CXX_FLAGS` variable. – Tsyvarev Mar 26 '19 at 22:49
  • @Tsyvarev thanks for the ```VERBOSE=1``` hint, I am going to try that right now. The ```DCMAKE_CXX_FLAGS``` are necessary because I generate both ```-m32``` and ```-m64``` in the ```build.sh```. Therefore I just added the ```-fPIC``` there. Is there any advantage of moving this flag into the ```CMakeLists.txt```? – Byomeer Mar 26 '19 at 23:00
  • @Tsyvarev like written in the question: I call CMake in my ```build.sh``` like this: ```cmake -DCMAKE_CXX_FLAGS="-m64 -fPIC" -DCMAKE_C_FLAGS="-m64 -fPIC"```. I found that to be the easiest way to differentiate between x64 and x86 builds. – Byomeer Mar 26 '19 at 23:20
  • Oh, sorry, missed that `-fPIC` set in the calling script. BTW, [idiomatic way](https://stackoverflow.com/questions/38296756/what-is-the-idiomatic-way-in-cmake-to-add-the-fpic-compiler-option) for set `-fPIC` flag in CMake is `set(CMAKE_POSITION_INDEPENDENT_CODE ON)`. And shared libraries has this flag set automatically. – Tsyvarev Mar 27 '19 at 07:50

0 Answers0