I am trying to compile the following code twice, once with -m32
and once without:
// File mylib.cc
#include <iostream>
void print_int_size() {
std::cout << sizeof(int*) << std::endl;
}
// File main.cc
void print_int_size();
int main() {
print_int_size();
return 0;
}
I have the following in my CMakeLists.txt:
project (Link32b VERSION 0.91 LANGUAGES CXX)
add_library ( mylib STATIC mylib.cc )
add_library ( mylib_32b STATIC mylib.cc )
target_compile_options ( mylib_32b PUBLIC -m32 )
add_executable ( main main.cc )
add_executable ( main_32b main.cc )
target_compile_options ( main_32b PRIVATE -m32 )
target_link_libraries ( main PRIVATE mylib )
target_link_libraries ( main_32b PRIVATE mylib_32b )
I get the following output when compiling (similar with gcc):
Scanning dependencies of target mylib
[ 12%] Building CXX object CMakeFiles/mylib.dir/mylib.cc.o
[ 25%] Linking CXX static library libmylib.a
[ 25%] Built target mylib
Scanning dependencies of target main
[ 37%] Building CXX object CMakeFiles/main.dir/main.cc.o
[ 50%] Linking CXX executable main
[ 50%] Built target main
Scanning dependencies of target mylib_32b
[ 62%] Building CXX object CMakeFiles/mylib_32b.dir/mylib.cc.o
[ 75%] Linking CXX static library libmylib_32b.a
[ 75%] Built target mylib_32b
Scanning dependencies of target main_32b
[ 87%] Building CXX object CMakeFiles/main_32b.dir/main.cc.o
[100%] Linking CXX executable main_32b
ld: warning: ignoring file CMakeFiles/main_32b.dir/main.cc.o, file was built for i386 which is not the architecture being linked (x86_64): CMakeFiles/main_32b.dir/main.cc.o
ld: warning: ignoring file libmylib_32b.a, file was built for archive which is not the architecture being linked (x86_64): libmylib_32b.a
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [main_32b] Error 1
make[1]: *** [CMakeFiles/main_32b.dir/all] Error 2
make: *** [all] Error 2
What am I missing here?
===
UPDATE: It is curious that setting CMAKE_CXX_FLAGS
to include -m32
makes the example work. However, I would like to get it done without setting variables, i.e., follow the mordern target-based approach.