xcode c++ project with lua lib got link error - c++

First, I create a c++ console project.
Then, I download the lua5.2.0 from lua.org, then make the lib use [make macosx test] and got the liblua.a.
Finally, I import the lua src into project and drag the liblua.a in project, write some demo code and compile,but get the link error, I confirm there has no grammar error.
Ld /Users/luocaixin/Library/Developer/Xcode/DerivedData/LuaAdapter-dwqvzxkkhfmcsneiftgqstjfemts/Build/Products/Debug/LuaAdapter normal x86_64
cd /Users/luocaixin/Desktop/xcodeWS1/LuaAdapter
export MACOSX_DEPLOYMENT_TARGET=10.10
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -L/Users/luocaixin/Library/Developer/Xcode/DerivedData/LuaAdapter-dwqvzxkkhfmcsneiftgqstjfemts/Build/Products/Debug -L/Users/luocaixin/Desktop/xcodeWS1/LuaAdapter/Lua.5.2.0/src -F/Users/luocaixin/Library/Developer/Xcode/DerivedData/LuaAdapter-dwqvzxkkhfmcsneiftgqstjfemts/Build/Products/Debug -filelist /Users/luocaixin/Library/Developer/Xcode/DerivedData/LuaAdapter-dwqvzxkkhfmcsneiftgqstjfemts/Build/Intermediates/LuaAdapter.build/Debug/LuaAdapter.build/Objects-normal/x86_64/LuaAdapter.LinkFileList -mmacosx-version-min=10.10 -stdlib=libc++ -llua -Xlinker -dependency_info -Xlinker /Users/luocaixin/Library/Developer/Xcode/DerivedData/LuaAdapter-dwqvzxkkhfmcsneiftgqstjfemts/Build/Intermediates/LuaAdapter.build/Debug/LuaAdapter.build/Objects-normal/x86_64/LuaAdapter_dependency_info.dat -o /Users/luocaixin/Library/Developer/Xcode/DerivedData/LuaAdapter-dwqvzxkkhfmcsneiftgqstjfemts/Build/Products/Debug/LuaAdapter
duplicate symbol _main in:
/Users/luocaixin/Library/Developer/Xcode/DerivedData/LuaAdapter-dwqvzxkkhfmcsneiftgqstjfemts/Build/Intermediates/LuaAdapter.build/Debug/LuaAdapter.build/Objects-normal/x86_64/lua.o
/Users/luocaixin/Library/Developer/Xcode/DerivedData/LuaAdapter-dwqvzxkkhfmcsneiftgqstjfemts/Build/Intermediates/LuaAdapter.build/Debug/LuaAdapter.build/Objects-normal/x86_64/luac.o
duplicate symbol _main in:
/Users/luocaixin/Library/Developer/Xcode/DerivedData/LuaAdapter-dwqvzxkkhfmcsneiftgqstjfemts/Build/Intermediates/LuaAdapter.build/Debug/LuaAdapter.build/Objects-normal/x86_64/lua.o
/Users/luocaixin/Library/Developer/Xcode/DerivedData/LuaAdapter-dwqvzxkkhfmcsneiftgqstjfemts/Build/Intermediates/LuaAdapter.build/Debug/LuaAdapter.build/Objects-normal/x86_64/main.o
ld: 2 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I want to know what cause this error and how to import lua in xcode c++
project correctly.

Did you read the error? It's telling you what's wrong: you have more than one main function. You've included two object files that contain a main function (lua, luac) and probably have one in your test app, too. You need to leave those files out of your project. They are the reference command line interpreter and compiler, respectively. You don't include them if you're embedding Lua as a library.

Related

vscode g++ Link failure : Undefined symbols for architecture x86_64

Basic Info:
system: macOS High Sierra(10.13.6)
editor : vs code(latest version)
Compiler: g++ (Xcode)
Target:deploy GLFW + GLAD
Question Description:
Recently, I'm learning to do some Computer Graphics related work. Everything is going smooth. However, when i create a window to test the env.Link error happened:
Undefined symbols for architecture x86_64:
"_gladLoadGLLoader", referenced from:
_main in main-5c211c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The terminal process terminated with exit code: 1
It seems I have not link some third party file. I
have fixed exactly the same problem by add "-lglfw" args to g++ when using functions in glfw3.h.
But when meeting glad related function : gladLoadGLLoader, I don't know how to do.
Something I have done:
Can find the head file.
#include glad/glad.h
#include GLFW/glfw3.h
Have put the file "glad.c" in workspace.
Try to Add "g++ -framework XXXXX" , But doesn't work.
Try to Add "g++ -lglfw3", But doesn't work.
Add "g++ -L or I /usr/lib or /usr/local/lib or /usr/local/include", But doesn't work.
Just tell the g++ to compile glad.c by adding "glad.c" to args. I thought glad.c will be compiled by default. Although I am not clear what happened, the problem is resolved anyway.
Add glad.c into Build Phases->Compile Sources

Library not found for -ljson11 but -lsqlite 3 is found

I'm trying to implement this tutorial with the addition that I want to include the json11 C++ library.
I followed the same process for both, downloaded the repo from:
https://github.com/libmx3/mx3, added them to the /deps/ folder of my project, and ran GYP to generate the .xcodeproj files for both.
Then I create a C++ project, add both the projects into the workspace, and add the libraries under Build Phases->Link binaries with libraries.
When I hit run the build fails with ld: library not found for -ljson11
I have tried regenerating them and adding them into the main xcodeproj. and into the Target Dependencies.
I have double-checked the library projects and they are identical. I have also added the folders where the .a files are compiled in Library Search paths but none of this makes a difference. If I remove the json11 dependency the project compiles, but I can't understand why the sqlite3 is found and throws no errors but the identical json11 project won't work.
I can see the .a files in the Debug-build folder and there are no errors if I build them separately.
Heres the full build log
Ld /Users/myuser/Library/Developer/Xcode/DerivedData/DjinniTest-eqaqzcfyskdxupbqpdngkdmoidtq/Build/Products/Debug/DjinniTest normal x86_64
cd /Users/myuser/Documents/Dev/djinni_test/cpp_project
export MACOSX_DEPLOYMENT_TARGET=10.11
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
-arch x86_64
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk
-L/Users/myuser/Library/Developer/Xcode/DerivedData/DjinniTest-eqaqzcfyskdxupbqpdngkdmoidtq/Build/Products/Debug
-L/Users/myuser/Library/Developer/Xcode/DerivedData/DjinniTest-eqaqzcfyskdxupbqpdngkdmoidtq/Build/Products/Debug/..
-F/Users/myuser/Library/Developer/Xcode/DerivedData/DjinniTest-eqaqzcfyskdxupbqpdngkdmoidtq/Build/Products/Debug
-filelist /Users/myuser/Library/Developer/Xcode/DerivedData/DjinniTest-eqaqzcfyskdxupbqpdngkdmoidtq/Build/Intermediates/DjinniTest.build/Debug/DjinniTest.build/Objects-normal/x86_64/DjinniTest.LinkFileList
-mmacosx-version-min=10.11
-stdlib=libc++
-ljson11
-lsqlite3
-Xlinker
-dependency_info
-Xlinker /Users/myuser/Library/Developer/Xcode/DerivedData/DjinniTest-eqaqzcfyskdxupbqpdngkdmoidtq/Build/Intermediates/DjinniTest.build/Debug/DjinniTest.build/Objects-normal/x86_64/DjinniTest_dependency_info.dat
-o /Users/myuser/Library/Developer/Xcode/DerivedData/DjinniTest-eqaqzcfyskdxupbqpdngkdmoidtq/Build/Products/Debug/DjinniTest
ld: library not found for
-ljson11
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Edit: even if I add the compiled files into the project so that they turn black, it still complains that the ljson11 library is not found. Maybe there's something wrong with the way the library is compiled?
Both the libraries gave warning that "macosx" was not found in the supported platforms. When I added it into the json11 project settings, the build compiled and both the warnings disappeared.
SQlite3 still only has iOS(iphone,iphonesimulator) but it compiles fine. I wonder what the difference is.

ld command on OSX

I wanted to use a Dynamic shared library ".so" file using ld command on Mac OS X.
I'm able to compile without errors and create the file using
ld -dynamic -dylib -arch x86_64 -macosx_version_min 10.10.0 -o libpacket.so obj/SpectrumClient.o obj/socket.o obj/packetCounters.o -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.1.0/lib/darwin/libclang_rt.osx.a
To check the file generated is for OS X..
~ vamsi$ file libpacket.so
libpacket.so: Mach-O 64-bit dynamically linked shared library x86_64
To allow ld to search for this I move the .so file to
/usr/lib
(I get error "ld: library not found for -lpacket" when I don't do so.)
To check if this works I run the command
ld -lpacket -v
I get the following error:
#(#)PROGRAM:ld PROJECT:ld64-242
configured to support archs: i386 x86_64 x86_64h armv6 armv7 armv7s armv7m armv7k arm64
Library search paths:
/usr/lib
/usr/local/lib
Framework search paths:
/Library/Frameworks/
/System/Library/Frameworks/
ld: warning: -arch not specified
ld: warning: -macosx_version_min not specified, assuming 10.6
Undefined symbols for architecture x86_64:
"start", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for inferred architecture x86_64
I'm running on updated OS X (10.10.5)
Even though I'm specifying the architecture and Minimum OS X version why am I getting the error?
I know I don't have start function in this .so file, but when I link this in other the functions are not linked.
Can someone point out the mistake I'm doing?
Thank you.
ld interprets your command ld -lpacket as a request to link a complete program executable from the object files in the specified library. That would make most sense if packet referred to a static library.
In any case, your library isn't sufficient to build a complete executable. The linker is implicitly looking for a start routine and not finding it. The start routine would not normally be in your code, it would be in the C runtime library. When you use the compiler to drive the link step, the compiler specifies the C runtime library, which provides start. However, the C runtime library then has a dependency on your code providing a _main symbol, so that just moves the problem one step along.
In short, ld -lpacket is not a sensible link command. You can't link just a dynamic library. What is it being linked to? What did you expect that command to accomplish?
The other stuff about -arch and -macosx_version_min not being specified is about this link command. The fact that you specified them when building the library is irrelevant. For this invocation of the linker, you're forcing the linker to guess what you intend, so it's telling you what it guessed.
I found out the issue with why the project wasn't able to link the .so file.
While compiling ".so" file, have additional config
-DTARGET_OS_MAC
Now I don't get the error "Can't find the method..." while linking the .so file to the other project.
#KenThomases.. You were right about "ld -lpacket -v" command not giving any useful information. Thank you!

Whenever I try to link a library I get an error

I'm trying to learn FLTK for C++ and I'm working on a Macbook with the clang++ compiler. Whenever I try to link the library like this:
clang++ -L/usr/local/lib -lfltk
It gives me this error:
clang: warning: argument unused during compilation: '-X11'
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)
I've looked on a couple other forums to see how to fix this problem and haven't gotten any answers that worked. I also tried reinstalling Command Line Tools from Apple's Developer site.
You have to make sure you put the linker command (-L/usr/local/lib -lfltk) before doing filename.cpp -o Exectuable but still in the same line in the command line.

Trying to compile tcpxtract on mac osx gives i386 symbol not found

I am doing some testing today and was searching for a command line program that would allow me to extract files out of tcp traffic, I seem to have found one called tcpxtract, however when I try to compile it, I get the following error on make
Undefined symbols for architecture i386:
"_yywrap", referenced from:
_yylex in confl.o
_input in confl.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
make: *** [tcpxtract] Error 1
What can I do to get it to compile?
I'm on a Macbook Pro Retina Early 2013 running 0SX 10.8.4
You need to link with -lfl (the flex library). Make sure it's after your object files.
Edit your Makefile after running configure. Go down to the line that says
LIBS = -lpcap
(circa line 130)
and change it to:
LIBS = -lpcap -ll
It should then build.