What is the idiomatic way in CMAKE to add the -fPIC compiler option?

問題:

I've come across at least 3 ways to do this and I'm wondering which is the idiomatic way.我遇到了至少 3 種方法來做到這一點,我想知道哪種是慣用的方法。 This needs to be done almost universally to any static library.這幾乎對任何靜態庫都需要完成。 I'm surprised that the Makefile generator in CMake doesn't automatically add this to static libraries.我很驚訝 CMake 中的 Makefile 生成器不會自動將它添加到靜態庫中。 (unless I'm missing something?) (除非我遺漏了什麼?)

target_compile_options(myLib PRIVATE -fPIC)

add_compile_options(-fPIC)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fpic")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fpic")

I believe there might also be other variations.我相信可能還有其他變化。 (please edit my question if you find one) (如果你找到一個,請編輯我的問題)

If you happen to know the answer to this question, do you also know if there is a way to cause a 3rd party CMake project to be compiled with this flag without modifying its CMakeLists.txt file?如果您碰巧知道這個問題的答案,您是否也知道是否有一種方法可以在不修改其 CMakeLists.txt 文件的情況下使用此標誌編譯 3rd 方 CMake 項目? I have run across static libraries missing that flag.我遇到過缺少該標誌的靜態庫。 It causes problems when compiling a static library into a dynamic library.將靜態庫編譯爲動態庫時會導致問題。

You get:你得到:

relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC

解決方案:

參考一: https://en.stackoom.com/question/2agjc
參考二: https://stackoom.com/question/2agjc
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章