embed lua runtime error: Symbol not found: _luaL_newstate - c++

my code
inline int DOFILE(string& filename) {
printf("lua_open\n");
/* initialize Lua */
lua_State* L = lua_open();
printf("lua_openlibs\n");
/* load Lua base libraries */
luaL_openlibs(L);
printf("lua_dofile\n");
/* run the script */
int ret = luaL_dofile(L, filename.c_str());
printf("lua_close\n");
/* cleanup Lua */
lua_close(L);
return ret;
}
compile options:
obj.cxxflags = ["-g", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE", "-Wall", "-llua-5.1"]
also tried '-llua', '-llualib', all of them report warning
i686-apple-darwin11-llvm-g++-4.2: -llua-5.1: linker input file unused because linking not done
When I run, it report:
lua_open
dyld: lazy symbol binding failed: Symbol not found: _luaL_newstate
Referenced from: /Users/gl/workspace/node-lua/build/Release/node_lua.node
Expected in: flat namespace
dyld: Symbol not found: _luaL_newstate
Referenced from: /Users/gl/workspace/node-lua/build/Release/node_lua.node
Expected in: flat namespace

You should be using the obj.ldflags parameter for libraries.
The build tool you are using produces its binaries in two steps:
compile
link
The compile step uses the obj.cxxflags compiler flags. Libraries are not needed to compile, so passing linker flags (-lfoo) in there is no useful - the compiler doesn't use them at all (hence the warnings).
The link step should use both obj.cxxflags and obj.ldflags. (ld is the name of the linker.)
(It is not uncommon for very simple code to do both compiling and linking at the same time, e.g. with g++ -o thing thing.cpp -lpthread. But for larger builds, separating compiling and linking is usual.)

Related

Turning symbol weak at link time to resolve multiple definitions

I am trying to decrease the binary size by overriding default __terminate_handler. Normally, when compiling with GCC, this piece of code:
[[noreturn]] void terminate() noexcept
{
while (true)
;
}
namespace __cxxabiv1
{
std::terminate_handler __terminate_handler = terminate;
}
Makes the default __terminate_handler overridden and the binary size decreases by 30-50 kB, depending on the release/debug build type.
When compiling with Clang I use -nostdlib linker flag, thus I have to link the standard libraries explicitly, by putting -lstdc++ -lgcc as the command line options. This means that I get an error:
ld.lld: error: duplicate symbol: __cxxabiv1::__terminate_handler
-> defined in armgnutoolchain-src/arm-none-eabi/lib/thumb/v7e-m+fp/hard/libstdc++.a(eh_term_handler.o)
-> defined in custom_terminate.cpp.obj
Both, custom code and libstdc++ define __cxxabiv1::__terminate_handler (_ZN10__cxxabiv119__terminate_handlerE) as a symbol initialized in data section. Ideally, I would like the symbol to be defined weak in the libstdc++.
Is there a way to ignore the symbol defined in libstdc++?

How to use and configure clang-tidy on windows?

I'm trying to use clang-tidy code analysis so I can check for CppCoreGuidelines. I downloaded LLVM 7.0.0 pre-built binary for Win 7 64 bits. I'm able to successfully compile with clang, I did a basic example compiling this code, I named the source test.cpp:
// test.cpp
#include <iostream>
int main(int argc, char const *argv[])
{
std::cout << "Hello World!" << std::endl;
return 0;
}
Then I ran this in the terminal:
clang test.cpp
I got this output when compiling:
test-c4b051.o : warning LNK4217: locally defined symbol __std_terminate imported in function "int `public: static unsigned __int64 __cdecl std::char_traits<char>::length(char const * const)'::`1'::dtor$2" (?dtor$2#?0??length#?$char_traits#D#std##SA_KQEBD#Z#4HA)
test-c4b051.o : warning LNK4217: locally defined symbol _CxxThrowException imported in function "public: void __cdecl std::ios_base::clear(int,bool)" (?clear#ios_base#std##QEAAXH_N#Z)
But it worked fine printing "Hello World" and everything goes fine until here, but when I want to run clang-tidy I get the following output when I run this, I took the reference from here Extra Clang Tools 8 documentation:
clang-tidy test.cpp -checks=-*,clang-analyzer-*,-clang-analyzer-cplusplus*
Error while trying to load a compilation database:
Could not auto-detect compilation database for file "test.cpp"
No compilation database found in C:\Users\uidr8361\Desktop\C++ or any parent directory
fixed-compilation-database: Error while opening fixed database: no such file or directory
json-compilation-database: Error while opening JSON database: no such file or directory
Running without flags.
I read this thread but this seems to apply for clang compilation and I don't know if this also applies for clang extra tools, clang-tidy in particular:
How to compile Clang on Windows
Just put -- (minus minus) on the command line at the end
clang-tidy -checks=-*,clang-analyzer-*,-clang-analyzer-cplusplus* test.cpp --
You would normally put your cl,gcc,clang arguments afterwards
clang-tidy -checks=-*,clang-analyzer-*,-clang-analyzer-cplusplus* test.cpp -- -DDEBUG -I./include

new c++ error with file

I am 100% new at c++ so bear with me :)
I am getting an error with this file and not sure why. any help is appreciated.
#include <iostream>
using namespace std;
int main()
{
cout << "hi" << endl;
return 0;
}
------------ Build: Debug in 1600 (compiler: GNU GCC Compiler)-------------
g++ -o bin/Debug/1600 obj/Debug/main.o obj/Debug/src/test.o obj/Debug/test03.o
duplicate symbol _main in:
obj/Debug/main.o
obj/Debug/test03.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
From the source files symbols are created. main in the .cpp file becomes _main as a symbol. During linking there can be only one main function, hence only one _main symbol is expected and allowed.
You have three object files that were created:
obj/Debug/main.o which contains main
obj/Debug/src/test.o
obj/Debug/test03.o which also contains main
Probably because you have a .cpp file for each of them and the command line or IDE you are using asked for them all to be compiled.
duplicate symbol _main
The text above is telling you that the linker (trying to make sense of all the compiled object (.o) files) found more than one main.
So the solution is to look at your IDE settings and remove the other files (or at least remove main from the other files) because you are only interested in compiling the one source file.
Its hard to tell what you're running from the question.
Here is how to build a simple C++ program using gcc
In
my_program.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "hi" << endl;
return 0;
}
To compile to object files type
g++ -c my_program.cpp
To link (you'd normally have more files here)
g++ -o my_program my_program.o
So, this isn't very fun so most people use a build system like make, cmake, msbuild or whatever the CLion IDE uses.

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.

Trouble setting up Lua in Xcode

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.