Skip to content

"duplicate symbol '_main'" on macOS when building tests #363

@stanislaw

Description

@stanislaw

Is your feature request related to a problem? Please describe.

This is one of the issues addressed by #352. I am extracting it hereby to be considered separately from the rest of my work of macOS port.

I am getting linker errors on macOS because the test targets link in libosal library that has a main() function and they link libut_bsp.a which has a main() that launches tests. This causes a conflict with these test target' own main() function as follows:

duplicate symbol '_main' in:
    libosal.a(bsp_start.c.o)
    libut_bsp.a(bsp_ut.c.o)

Describe the solution you'd like

Usually the cases like this resolved by extracting the main function from one of the conflicting libraries. In this case it could make sense to extract the main function from libosal to a separate library such as libosal-init or libosal-main to avoid any conflicts with test targets. That separate library would then not be linked to the tests and the conflict would go away.

Describe alternatives you've considered

There seems to be no alternative to this behavior on macOS. You either have one main() function or there is a conflict.

Additional context

This is a more complete fragment of the output:

[9/17] Linking C executable timer-test
FAILED: timer-test
: && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -D_XOPEN_SOURCE=600    -D_LINUX_OS_ -Wl,-search_paths_first -Wl,-headerpad_max_install_names -u _OS_VolumeTable -u _OS_Application_Startup tests/CMakeFiles/timer-test.dir/timer-test/timer-test.c.o  -o timer-test  libut_assert.a libosal.a libut_bsp.a -lpthread -ldl posix-mac-addons/src/mqueue/libmqueue.a posix-mac-addons/src/stubs/librt.a posix-mac-addons/src/pthread/libposix-mac-pthread.a posix-mac-addons/src/semaphore/libposix-mac-semaphore.a posix-mac-addons/src/stubs/libposix-mac-stubs.a posix-mac-addons/src/time/libposix-mac-time.a posix-mac-addons/src/timer/libposix-mac-timer.a && :
duplicate symbol '_main' in:
    libosal.a(bsp_start.c.o)
    libut_bsp.a(bsp_ut.c.o)
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[10/17] Linking C executable bin-sem-test
FAILED: bin-sem-test
: && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -D_XOPEN_SOURCE=600    -D_LINUX_OS_ -Wl,-search_paths_first -Wl,-headerpad_max_install_names -u _OS_VolumeTable -u _OS_Application_Startup tests/CMakeFiles/bin-sem-test.dir/bin-sem-test/bin-sem-test.c.o  -o bin-sem-test  libut_assert.a libosal.a libut_bsp.a -lpthread -ldl posix-mac-addons/src/mqueue/libmqueue.a posix-mac-addons/src/stubs/librt.a posix-mac-addons/src/pthread/libposix-mac-pthread.a posix-mac-addons/src/semaphore/libposix-mac-semaphore.a posix-mac-addons/src/stubs/libposix-mac-stubs.a posix-mac-addons/src/time/libposix-mac-time.a posix-mac-addons/src/timer/libposix-mac-timer.a && :
duplicate symbol '_main' in:
    libosal.a(bsp_start.c.o)
    libut_bsp.a(bsp_ut.c.o)
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

Requester Info

Stanislav Pankevich

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions