Linking issue on MacOS Catalina (using XCode)

189 views
Skip to first unread message

Samyukta Yagati

unread,
Sep 15, 2021, 6:39:42 PM9/15/21
to Crypto++ Users
I installed Crypto++ from source as follows:
  1. git clone https://github.com/weidai11/cryptopp.git
  2. cd cryptopp
  3. Uncommented the CXX flag line in GNUmakefile that adds "-stdlib=libc++" because I am using XCode. 
  4. make -j 8 (8 = value of $(nproc) on my machine if it were linux)
  5. sudo make install
The make and install complete without errors. I confirmed that /usr/local/lib contains libcryptopp.a.

When I try to use libcryptopp in a project (in a different directory from the cryptopp install), clang (Apple Clang 12.0.0) can't find it. I get the following error message:

+ LD     lib/crypto_bench
ld: library not found for -l:libcryptopp.a
clang: error: linker command failed with exit code 1 (use -v to see invocation)

This is the output of gcc --version (gcc is linked to clang but not directly used):

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1

Apple clang version 12.0.0 (clang-1200.0.32.29)

Target: x86_64-apple-darwin19.6.0

Thread model: posix

InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

I also tried updating my PATH variable in bash_profile with the location of libcryptopp as follows, then source'd bash_profile again:

export PATH="/usr/local/lib:$PATH"

However, this resulted in the same error as before when I tried to compile the project. I've spent several hours googling and haven't been able to find a fix, and I'm wary of manually symlinking the file because it can break other installs on my laptop. Any help with fixing this issue would be much appreciated.

Jeffrey Walton

unread,
Sep 15, 2021, 7:58:05 PM9/15/21
to Crypto++ Users List
On Wed, Sep 15, 2021 at 6:39 PM Samyukta Yagati <sa...@berkeley.edu> wrote:
>
> I installed Crypto++ from source as follows:
>
> git clone https://github.com/weidai11/cryptopp.git
> cd cryptopp
> Uncommented the CXX flag line in GNUmakefile that adds "-stdlib=libc++" because I am using XCode.
> make -j 8 (8 = value of $(nproc) on my machine if it were linux)
> sudo make install
>
> The make and install complete without errors. I confirmed that /usr/local/lib contains libcryptopp.a.
>
> When I try to use libcryptopp in a project (in a different directory from the cryptopp install), clang (Apple Clang 12.0.0) can't find it. I get the following error message:
>
> + LD lib/crypto_bench
> ld: library not found for -l:libcryptopp.a
> clang: error: linker command failed with exit code 1 (use -v to see invocation)
>
> ...
>
> I also tried updating my PATH variable in bash_profile with the location of libcryptopp as follows, then source'd bash_profile again:
>
> export PATH="/usr/local/lib:$PATH"
>
> However, this resulted in the same error as before when I tried to compile the project. I've spent several hours googling and haven't been able to find a fix, and I'm wary of manually symlinking the file because it can break other installs on my laptop. Any help with fixing this issue would be much appreciated.

When linking, use -L to provide a path to the linker. That is, you
invoke ld directly. If you are passing the linker option through the
compiler driver, then use -Wl,-L -Wl,<path>

So you would probably use something like:

clang++ -g2 -O3 -I /usr/local/include test.cxx -Wl,-L
-Wl,/usr/local/lib -o test.exe

You would have to do the same on, say, CentOS, Fedora and Red Hat. Red
Hat does not put /usr/local on-path for the build tools.

Jeff

Samyukta Yagati

unread,
Sep 16, 2021, 3:04:00 PM9/16/21
to Crypto++ Users
I'm new to using clang, so to clarify: in my case, I should try "clang++ -g2 -O3 -I /usr/local/include cryptopp -Wl,-L" (where cryptopp is a subdirectory of include)? Also, is "-Wl,/usr/local/lib -o test.exe" intended to be shorthand for "clang++ -g2 -O3 -Wl,/usr/local/lib -o test.exe"? (and, relatedly, what output should I expect in test.exe?)

Samyukta Yagati

unread,
Sep 16, 2021, 6:22:33 PM9/16/21
to Crypto++ Users
As a follow up: I tried running 

>> clang++ -g2 -O3 -l /usr/local/include /usr/local/lib/libcryptopp.a -WI,-L

And got the error:

ld: library not found for -l/usr/local/include

clang: error: linker command failed with exit code 1 (use -v to see invocation)

It seems like the intention of this command was to ask clang to link to the include path to crypto++ but clang doesn't have the include path on in its build tools? Further suggestions or clarifications would be great, I tried googling again and couldn't find anything.

Jeffrey Walton

unread,
Sep 16, 2021, 6:56:20 PM9/16/21
to Crypto++ Users List
On Thu, Sep 16, 2021 at 6:22 PM Samyukta Yagati <sa...@berkeley.edu> wrote:
>
> As a follow up: I tried running
>
> >> clang++ -g2 -O3 -l /usr/local/include /usr/local/lib/libcryptopp.a -WI,-L
>
> And got the error:
> ld: library not found for -l/usr/local/include
> clang: error: linker command failed with exit code 1 (use -v to see invocation)

That's an "I" as in "include", bot a lowercase "L".

Use this command:

clang++ -g2 -O3 -I /usr/local/include test.cxx
/usr/local/lib/libcryptopp.a -o test.exe

An archive like libcryptopp.a is just a collection of object files. It
is equivalent to saying:

clang++ -g2 -O3 -I /usr/local/include test.cxx
/usr/local/lib/cryptlib.o /usr/local/lib/cpu.o
/usr/local/lib/integer.o ... -o test.exe

A share object like libcryptopp.dylib or libcryptopp.so is the same
collection of object files in an executable format that can be loaded
at runtime.

Jeff

Samyukta Yagati

unread,
Sep 16, 2021, 7:28:56 PM9/16/21
to Crypto++ Users
I think I'm still misunderstanding the intention of the command. 

On Thursday, September 16, 2021 at 3:56:20 PM UTC-7 Jeffrey Walton wrote:

That's an "I" as in "include", bot a lowercase "L".

Use this command:

clang++ -g2 -O3 -I /usr/local/include test.cxx
/usr/local/lib/libcryptopp.a -o test.exe

For the above command (I'm reading it as a single line, so all part of one command), is the test.cxx file something that's generated by the cryptopp install process? If so, which directory should I be running this from? (I don't see a test.cxx in the cryptopp directory and searching online suggests this is related to the google testing framework for C++).
 
If I run the following (without the test.cxx file as input, which probably doesn't make sense but again I couldn't tell what function it's serving):
>> clang++ -g2 -O3 -I /usr/local/include /usr/local/lib/libcryptopp.a -o test.exe

I get this error:

Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64

Thank you for your help so far, I really appreciate it.

Uri Blumenthal

unread,
Sep 16, 2021, 9:31:25 PM9/16/21
to Samyukta Yagati, Crypto++ Users
Say, you have a file that includes Crypto++ headers and needs linking with Crypto++ library. And Crypto++ is installed into /usr/local.

Then, the following would be sufficient:

clang++ -o my_binary -I/usr/local/include my_source.cpp -L /usr/local/lib -lcryptopp -lm

(I'm not sure if "-lm" is still necessary)


> On Sep 16, 2021, at 19:28, Samyukta Yagati <sa...@berkeley.edu> wrote:
>

Samyukta Yagati

unread,
Sep 17, 2021, 4:38:31 PM9/17/21
to Crypto++ Users
Ah I see -- I'm using make with a custom makefile to build the source files in my project, is there a way to translate this to flags in the makefile?

Samyukta Yagati

unread,
Sep 17, 2021, 4:39:53 PM9/17/21
to Crypto++ Users
(I currently have -l:libcryptopp.a in my LDFLAGS)

Uri Blumenthal

unread,
Sep 17, 2021, 6:11:53 PM9/17/21
to cryptop...@googlegroups.com
Sorry, I've no idea what your makefile looks like, or how to convey the working flags to it. 

On Sep 17, 2021, at 16:39, Samyukta Yagati <sa...@berkeley.edu> wrote:

(I currently have -l:libcryptopp.a in my LDFLAGS)
--
You received this message because you are subscribed to the Google Groups "Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cryptopp-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cryptopp-users/436b6393-8bb5-4b5e-b50c-1bb71da7aa24n%40googlegroups.com.

Samyukta Yagati

unread,
Sep 17, 2021, 7:04:36 PM9/17/21
to Crypto++ Users
Thanks for your help! Resolved, the issue was in my Makefile flags.
Reply all
Reply to author
Forward
0 new messages