Trouble setting up Lua in Xcode - c++

How can I include Lua in my project in Xcode?
I have installed Lua via the instructions on the website (curl, extract, make macosx install, etc).
I can reference lua
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
But when I go to use it, I get an error (even writing something as simple as the following)
lua_State *L = luaL_newstate();
lua_close(L);
It tells me :
Undefined symbols for architecture x86_64:
"_luaL_newstate", referenced from:
_main in main.o
"_lua_close", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Any advice would be much appreciated. If you need further information, let me know and I can edit it in. Thanks!
EDIT:
Added the -llua flag (SFML included the /usr/local/lib and include). Now getting "ignoring file /usr/local/lib/liblua.a, file was built for archive which is not the architecture being linked (i386): /usr/local/lib/liblua.a"
EDIT2:
I changed Base SDK to Latest OS X (OS X 10.9) and Build Active Architecture Only to "Yes" and now it will compile.

In Xcode, select < ProjectName > with blue icon on top of the left pane (where all the sources are), then in main window select a target under Targets. In Build Settings tab, select All instead of Basic and set following parameters:
Other Linker Flags = -llua
Header Search Paths = /usr/local/include
Library Search Paths = /usr/local/lib
Assuming Lua headers were installed in /usr/local/include, and liblua.a in /usr/local/lib.
You may also use search field to find them.

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

PostgreSQL external C function link failed on Mac OSX

I'm trying to build an external PostgreSQL function on OSX 10.11 with both clang and gcc, but link failed with the following errors:
c++ -I/usr/local/Cellar/postgresql/9.5.3/include/server -fpic -c ./main.c
c++ -shared -o ttt.dylib main.o
Undefined symbols for architecture x86_64:
"_deconstruct_array", referenced from:
_psql_nearest in main.o
"_elog_finish", referenced from:
_psql_nearest in main.o
"_elog_start", referenced from:
_psql_nearest in main.o
"_get_typlenbyvalalign", referenced from:
_psql_nearest in main.o
"_pfree", referenced from:
_psql_nearest in main.o
"_pg_detoast_datum", referenced from:
_psql_nearest in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
It looks like I need to link my library with some of PostgreSQL libraries. What are these libraries?
main.cpp:
extern "C" {
#include <postgres.h>
#include <fmgr.h>
#include <utils/array.h>
#include <utils/lsyscache.h>
#include <catalog/pg_type.h>
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
PG_FUNCTION_INFO_V1(psql_nearest);
Datum psql_nearest(PG_FUNCTION_ARGS) {
if(PG_ARGISNULL(0) || PG_ARGISNULL(1)) {
elog(ERROR, "DOC2VEC: NULL INPUT DATA");
PG_RETURN_NULL();
}
ArrayType *_docVector = PG_GETARG_ARRAYTYPE_P(0);
Oid elTypeVals = ARR_ELEMTYPE(_docVector);
if (elTypeVals != FLOAT4OID) {
elog(ERROR, "DOC2VEC: INVALID INPUT DATA TYPE");
PG_RETURN_NULL();
}
int16 typeLenVals = 0;
bool typeByValVals = false;
char typeAlignVals = char(0);
get_typlenbyvalalign(elTypeVals, &typeLenVals, &typeByValVals, &typeAlignVals);
Datum *inputVals;
bool *nullVals;
int nVals;
deconstruct_array(_docVector, elTypeVals, typeLenVals, typeByValVals, typeAlignVals, &inputVals, &nullVals, &nVals);
pfree(inputVals);
pfree(nullVals);
PG_RETURN_NULL();
}
}
Thanks to PostgreSQL developers, they explained me the difference in Linux and OSX linking of external functions.
Instead of -shared you need -bundle -bundle_loader /path/to/postgres,
and there are some other linker flags that are advisable too.
Also, PostgreSQL expects the file extension for loadable modules to be .so even on OSX.
It's usually better to use PGXS to build extensions, instead of
learning such details for yourself.
Or you can crib from one of the extensions in the contrib/
source tree.
If you need to link, you will need the -L flag to point the linker to the path where the postgres libraries are located (the linker equivalent of the -I compiler flag). and the -l flag to actually link the libraries (one for each library); the library name without the lib prefix and without the extension.
In your case, something along the lines of -L/usr/local/Cellar/postgresql/9.5.3/lib -lpostgres
(There's a variety of library files in that directory; try -lpg to start with.
The reference to _pfree in your error message also suggest to link pgcommon, which contains the implementation of pgree (at least when using nm libpgcommon.a).
)
You may want to read up a bit more on compiling and linking in general; you do the right thing for compiling with the -I flag, but oddly then miss out on the linking step. And learning about make and Makefiles will come in handy.
I also don't understand the extern "C" { part for a .c file, which is clearly a C-only file. extern "C" is usually used in C++ files for compatibility with C.

how to use object-Cpp in Xcode

I created a new cocoa application in Xcode with 3 *.m files (main.m, AppDelegate.m, and projectnameTests.m).
In order to to use object-cpp, I renamed the 3 *.m files to *.mm files.
Then I get this following error from Xcode:
"Undefined symbols for architecture x86_64:
"_NSApplicationMain", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)"
I have never dealt .o files, so am not sure what's wrong.
Please help.
P.S.: I use the latest Xcode, version(6.1.1).
Add AppKit.framework to the "Link Binary With Libraries" section under the Build Phases for your project target, and that should fix the problem.
P.S. Don't ask me why XCode automatically finds the necessary framework (i.e. AppKit.framework) for .m files but not for .mm files, but adding AppKit.framework fixes the issue because NSApplicationMain is defined in AppKit. Adding Cocoa.framework also fixes the issue, likely because it triggers XCode to automatically link with AppKit.framework.

Linker Flag/Boost not found -- linking Boost filesystem with Xcode

I am trying to the use Boost Filesystem library in Xcode.
I installed Boost with brew install boost, and it was successful.
Turns out Boost wasn't linked so I ran brew link --overwrite Boost, which worked.
I am trying to #include <boost/filesystem.hpp> in my project, but have not been successful so far.
At first Xcode couldn't find <boost/filesystem.hpp>, so I added
/usr/local/lib
/usr/local/include
to my Header search paths for Target and Project.
That fixed the original problem, but then boost_system could not be found by Xcode.
Here is the transcript:
Undefined symbols for architecture x86_64:
"boost::system::system_category()", referenced from:
___cxx_global_var_init2 in main.o
"boost::system::generic_category()", referenced from:
___cxx_global_var_init in main.o
___cxx_global_var_init1 in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I added the -lboost_system linker flag to my project, and now I am getting this error:
ld: library not found for -lboost_system
clang: error: linker command failed with exit code 1 (use -v to see invocation)
And it's definitely the correct linker flag, so perhaps Boost installed incorrectly after all.
I will update this question if I find a solution, but I would appreciate help in solving this problem.
You only need /usr/local/include in your header search path and you need /usr/local/lib in your library search path in order for the linker to find the libraries.
Had the same problem in xcode 6 . user header search path and library search was okay . still showed link error . I solved the problem by adding -lboost_system-mt in other linker flag in project target setting

sql::mysql::get_driver_instance() Symbol not found

I'm trying to link the mysql native API to my server software now... With little luck. When compiling in Xcode on OSX10.9 using the flags
$(OTHER_CFLAGS) -I /usr/local/boost_1_55_0/ -I
/usr/local/mysqlconn/include/ -I /usr/local/mysqlconn/lib/
I get the compiler/linker error:
Undefined symbols for architecture x86_64:
"sql::mysql::get_driver_instance()", referenced from:
sql::mysql::get_mysql_driver_instance() in SQLComm.o ld: symbol(s) not found for architecture x86_64 clang: error: linker
command failed with exit code 1 (use -v to see invocation)
I really have no idea why though. The API files were downloaded by me today, and extracted at the included paths. I include all the files from include and include/cppconn in the server program that uses the function the linker cannot found.
Any ideas what I am doing wrong?
I see you configuring include directories, but I don't see any libraries listed there.
You need to link in the library when you build your project.