alliedmodders / sourcemod

SourceMod - Source Engine Scripting and Administration
http://www.sourcemod.net/
975 stars 422 forks source link

AMBuild does not link SDK libraries when using GCC with "as-needed" global linker configuration #1308

Open bottiger1 opened 4 years ago

bottiger1 commented 4 years ago

Using the latest sample extension makes extensions that do not link to the shared libraries like libtier0_srv.so correctly. If your extension uses anything from libtier0_srv.so, it will print this when attempting to load the extension:

"undefined symbol: _AssertValidWritePtr"

[11950] c++ _home_tf2_extensions_sdk_sourcemod_public_smsdk_ext.o extension.o sha256.o libvstdlib_srv.so libtier0_srv.so -m32 -Wl,--exclude-libs,ALL -lm -static-libgcc /home/tf2/extensions/sdk/hl2sdk-tf2/lib/linux/tier1_i486.a /home/tf2/extensions/sdk/hl2sdk-tf2/lib/linux/mathlib_i486.a -shared -o sample.ext.2.tf2.so

ldd sample.ext.2.tf2.so linux-gate.so.1 (0xf7fb4000) libstdc++.so.6 => /usr/lib/i386-linux-gnu/libstdc++.so.6 (0xf7e0b000) libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xf7c2f000) libm.so.6 => /lib/i386-linux-gnu/libm.so.6 (0xf7b2d000) /lib/ld-linux.so.2 (0xf7fb6000) libgcc_s.so.1 => /lib/i386-linux-gnu/libgcc_s.so.1 (0xf7b0f000)

By putting libtier0_srv.so at the end before -shared, it will correctly link to the library and load properly. I am not sure how to fix this in ambuild as it is a build system I am unfamiliar with.

Here are the GCC versions I have.

Checking CC compiler (vendor test gcc)... ['cc', 'test.c', '-o', 'test'] found gcc version 7.4 Checking CXX compiler (vendor test gcc)... ['c++', 'test.cpp', '-o', 'testp'] found gcc version 7.4

bottiger1 commented 4 years ago

Found a simple fix by adding this line to the AMBuildScript

for library in dynamic_libs:
  source_path = os.path.join(lib_folder, library)
  output_path = os.path.join(binary.localFolder, library)
  compiler.postlink.append(library) # new line
asherkin commented 4 years ago

This is a known issue with using GCC on certain Linux distributions that customise the default linker flags. Using Clang is recommended.

bottiger1 commented 4 years ago

How exactly do you specify clang? When I ran the script it automatically chose GCC. And why not fix it to work for both compilers?

peace-maker commented 4 years ago

You do CC=clang CXX=clang++ python ../configure.py --options

bottiger1 commented 4 years ago

In file included from /home/tf2/extensions/sample/extension.cpp:36: In file included from /home/tf2/extensions/sdk/hl2sdk-tf2/public/inetchannel.h:15: In file included from /home/tf2/extensions/sdk/hl2sdk-tf2/public/tier1/bitbuf.h:19: In file included from /home/tf2/extensions/sdk/hl2sdk-tf2/public/mathlib/mathlib.h:12: /home/tf2/extensions/sdk/hl2sdk-tf2/public/mathlib/vector.h:224:5: error: macro expansion producing 'defined' has undefined behavior [-Werror,-Wexpansion-to-defined]

if USE_M64S

^

/home/tf2/extensions/sdk/hl2sdk-tf2/public/mathlib/vector.h:207:23: note: expanded from macro 'USE_M64S'

define USE_M64S ( ( !defined( _X360 ) ) && ( ! defined( _LINUX) ) )

                  ^

/home/tf2/extensions/sdk/hl2sdk-tf2/public/mathlib/vector.h:224:5: error: macro expansion producing 'defined' has undefined behavior [-Werror,-Wexpansion-to-defined] /home/tf2/extensions/sdk/hl2sdk-tf2/public/mathlib/vector.h:207:49: note: expanded from macro 'USE_M64S'

define USE_M64S ( ( !defined( _X360 ) ) && ( ! defined( _LINUX) ) )

                                            ^

/home/tf2/extensions/sdk/hl2sdk-tf2/public/mathlib/vector.h:281:5: error: macro expansion producing 'defined' has undefined behavior [-Werror,-Wexpansion-to-defined]

if USE_M64S

^

/home/tf2/extensions/sdk/hl2sdk-tf2/public/mathlib/vector.h:207:23: note: expanded from macro 'USE_M64S'

define USE_M64S ( ( !defined( _X360 ) ) && ( ! defined( _LINUX) ) )

                  ^

/home/tf2/extensions/sdk/hl2sdk-tf2/public/mathlib/vector.h:281:5: error: macro expansion producing 'defined' has undefined behavior [-Werror,-Wexpansion-to-defined] /home/tf2/extensions/sdk/hl2sdk-tf2/public/mathlib/vector.h:207:49: note: expanded from macro 'USE_M64S'

define USE_M64S ( ( !defined( _X360 ) ) && ( ! defined( _LINUX) ) )

4 errors generated. Build failed.

Headline commented 3 years ago

Closing this as stale - if this issue should still be considered please reopen 👍

asherkin commented 3 years ago

This is something worth fixing - I've reworded the title to cover the general issue.

bottiger1 commented 3 years ago

Closing this as stale - if this issue should still be considered please reopen 👍

This issue is not solved and specifying clang results in the error message above when you try to include anything from hl2sdk.

I'd recommend adding my hack into AMBuildScript to save new extension developers from wasting time imo. Or if you want to tell them to use clang, then the script should squelch Wexpansion-to-defined errors.