c++ - CMake compile project with multiple mains and shared functions -


let's suppose have 2 source files a , b. each 1 has main , public function.

a                 b |__ main()        |__ main() |__ foo()         |__ bar() 

i want bar method use foo function. how can compile in cmake project?

with configuration, b obviusly doesn't know a's foo function.

cmake_minimum_required(version 3.5) project(my_project)  set(cmake_cxx_flags "${cmake_cxx_flags} -std=c++14 -o3")  add_executable(a a.cpp) add_executable(b b.cpp) 

this configuration has obviusly both definitions of main.

cmake_minimum_required(version 3.5) project(my_project)  set(cmake_cxx_flags "${cmake_cxx_flags} -std=c++14 -o3")  set(source_files a.cpp) add_library(a_lib static ${source_files})  add_executable(a a.cpp) add_executable(b b.cpp)  target_link_libraries(b a_lib) 

is there way achieve want? i'd not combine both mains, or separate foo , a main in 2 different files. maybe using c++ headers?

split a 2 source files:

  • one containing main function, , used a executable target not library.

  • one containing foo function, used a_lib library target.

then use library a executable target too.

that's "best" solution imo.

you can use preprocessor macros conditionally compile main function in a.cpp. it's not recommend.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -