0

I have been building multiple projects that require sse2 instruction set. Adding -march=native as mentioned in sse2 instruction set not enabled has done the job till now.

However, in the 3 projects I have needed this, the gcc commands in the makefile were easy to locate and editing slightly the makefile worked. Now I tackle a far more complex makefile generated by cmake. I believe it makes more makefiles in other folders and then runs them etc. So just adding a simple -march=native is neither desirable nor easy and will take a considerable amount of time, if it is feasible afterall.

Is there a way to somehow always have sse2 enabled, opposite to now? I googled this but wasn't able to find something.

Given the fact that non of the makefiles I downloaded from github had -march=native inside, it leads me to believe that indeed there might be some way to set this on gcc/g++ level beforehand?

I found this -mfpmath=sse flag but was not able to formulate a correct gcc/g++ command (if that is related).

Can you help me?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Paramar
  • 287
  • 1
  • 5
  • 22
  • 1
    Instead of editing the generated makefiles, you should add `-march=native` to the `CMAKE_CXX_FLAGS` variable in the `CMakeLists.txt` file before generating the makefiles. – eike May 21 '21 at 12:47
  • could you please give the exact code? If it works, also please provide your answer as a separate answer so I can accept it. To help you, CXX_FLAGS is not even set in the CMakeLists.txt for the moment – Paramar May 21 '21 at 13:01
  • SSE2 is baseline for x86-64, i.e. a normal compile on a modern system (making a 64-bit executable) already includes `-msse2` on by default. You only need it manually if you're building obsolete 32-bit binaries. (Although you should still use `-march=native` if you're only going to run your binary on this computer, or newer ones like it.) – Peter Cordes May 21 '21 at 17:13

1 Answers1

3

You can add the compiler option -march=native in CMakeLists.txt

SET(PROJECT_FLAGS "-march=native")
SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${PROJECT_FLAGS }")

Or

add_compile_options(-march=native) # CMake 2.8.12 or newer
Altaf
  • 2,838
  • 1
  • 16
  • 8
  • second proposition worked. First one not ( probably I added it in the wrong place in the file?) Anyways, thanks a lot Altaf, accepted answer. – Paramar May 21 '21 at 13:20