I have two files main.cpp and simd.cpp. I compile them like this:
g++ -c -mavx -O3 simd.cpp -o simd_avx.o
g++ -c -msse2 -O3 simd.cpp -o simd_sse2.o
g++ -O3 main.cpp simd_avx.o simd_sse2.o
Now I want to build this with Cmake. Here is my CMakeLists.txt file:
add_library(avx OBJECT simd.cpp)
add_library(sse2 OBJECT simd.cpp)
IF(MSVC)
set(COMPILE_FLAGS "/openmp /Wall")
set_target_properties (avx PROPERTIES COMPILE_FLAGS "/arch:AVX")
set_target_properties (sse2 PROPERTIES COMPILE_FLAGS "/arch:SSE2 -D__SSE2__")
set(CMAKE_CXX_FLAGS ${COMPILE_FLAGS})
set(CMAKE_CXX_FLAGS_RELEASE "${COMPILE_FLAGS}")
ELSE()
set(COMPILE_FLAGS "-fopenmp -Wall")
set_target_properties (avx PROPERTIES COMPILE_DEFINITIONS "-mavx")
set_target_properties (sse2 PROPERTIES COMPILE_DEFINITIONS "-msse2")
set(CMAKE_CXX_FLAGS ${COMPILE_FLAGS})
set(CMAKE_CXX_FLAGS_RELEASE "${COMPILE_FLAGS} -O3")
ENDIF()
add_executable(dispatch main.cpp $<TARGET_OBJECTS:avx> $<TARGET_OBJECTS:sse2>)
With MSVC2012 this works fine. However, it makes two separate folders in the build file, sse and avx, which each point to the same simd.cpp. I guess this is a fine solution since I can set the properties separately for each folder. It's a bit annoying having the same source file displayed twice.
But when I make a makefile for GCC in Linux I get the error:
<command-line>:0:1: error: macro names must be identifiers
make[2]: *** [src/CMakeFiles/avx.dir/simd.cpp.o] Error 1
make[1]: *** [src/CMakeFiles/avx.dir/all] Error 2
make: *** [all] Error 2
Can you explain why the Makefile is failing? Can you suggest a better CMakeLists.txt file to do what I want to do?
simd.cpp
#include <stdio.h>
#ifdef __AVX__
void func_avx() {
printf("avx\n");
}
#elif defined ( __SSE2__ )
void func_sse2() {
printf("sse2\n");
}
#else
// scalar code
#endif
main.cpp
extern void func_sse2();
extern void func_avx();
void func_dispatch();
void (*fp)() = &func_dispatch;
void func_dispatch() {
#ifdef __AVX__
fp = func_avx;
#else
fp = func_sse2;
#endif
fp();
}
void func() {
fp();
}
int main() {
func();
}