II
Size: a a a
II
EE
$ cat CMakeLists.txtпочему здесь нет бабаха про
cmake_minimum_required(VERSION 3.15)
project(link)
add_executable(test main.cpp foo-test.cpp)
add_library(lib foo-real.cpp)
target_link_libraries(test PRIVATE lib)
$ cat main.cpp
#include "foo.h"
int main() { return foo(); }
$ cat foo.h
#pragma once
int foo();
$ cat foo-real.cpp
int foo() { return 0; }
$ cat foo-test.cpp
int foo() { return 1; }
$ cmake --build b
[5/5] Linking CXX executable test
$ ./b/test; echo $?
1
multiple definitions: foo@real.cpp, foo@test.cpp
?..AF
AF
II
$ uname -a
Linux workpc 4.19.152-1-MANJARO #1 SMP Sat Oct 17 10:51:27 UTC 2020 x86_64 GNU/Linux
AF
AF
II
II
The reason for the difference in functionality is that there are different types of symbols.
A library function is a weak symbol. It is only included if it is not already defined.
AT
add_executable(test
main.cpp
foo-test.cpp
foo-real.cpp
)
$ cmake --build b
[1/1] Linking CXX executable test
FAILED: test
: && /usr/bin/clang++ CMakeFiles/test.dir/main.cpp.o CMakeFiles/test.dir/foo-test.cpp.o CMakeFiles/test.dir/foo-real.cpp.o -o test && :
/usr/bin/ld: CMakeFiles/test.dir/foo-real.cpp.o: in function `foo()':
foo-real.cpp:(.text+0x0): multiple definition of `foo()'; CMakeFiles/test.dir/foo-test.cpp.o:foo-test.cpp:(.text+0x0): first defined here
multiple definition
, но в твоём случае её нет, т.к. все символы из библиотеки есть и в локальных объектных файлах.II
multiple definition
, но в твоём случае её нет, т.к. все символы из библиотеки есть и в локальных объектных файлах.AT
The reason for the difference in functionality is that there are different types of symbols.
A library function is a weak symbol. It is only included if it is not already defined.
Weak symbol
это отдельный механизм связывания, там по ссылке в wiki можно почитать, не имеет отношения к обычным библиотекам в привычном понимании.АС