Not able to compile with curses on gcc (OS X)) - compilation

I wrote a hello world program to see how curses library works.
Here is my program:
/Users/snihalani/dev/daas at 10:10AM
➜ cat main.c
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
int main(void)
{
int returnValue = 0;
while(1)
{
printf("I got %d\n", getch());
}
return 0;
}
I ran gcc main.c
I got
/Users/snihalani/dev/daas at 10:14AM
➜ gcc main.c
Undefined symbols for architecture x86_64:
"_stdscr", referenced from:
_main in ccEvUdhx.o
"_wgetch", referenced from:
_main in ccEvUdhx.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
I don't what's going wrong. Can anyone please help?

Nevermind. I had to add -lcurses option while compiling.

Related

gcc error on trying to compile lua file

I am trying to create executable from lua file using following method:
I use bintocee utility (from: http://lua-users.org/wiki/BinToCee) to convert myfile.lua to code.c . I then use following main.c (from: Creating standalone Lua executables)
#include <stdlib.h>
#include <stdio.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
int main(int argc, char *argv[]) {
int i;
lua_State *L = luaL_newstate();
luaL_openlibs(L);
lua_newtable(L);
for (i = 0; i < argc; i++) {
lua_pushnumber(L, i);
lua_pushstring(L, argv[i]);
lua_rawset(L, -3);
}
lua_setglobal(L, "arg");
#include "code.c"
lua_close(L);
return 0;
}
Then I give command:
gcc main.c -o myfile.exe
However, I get following error:
/tmp/ccyIOC0O.o: In function `main':
main.c:(.text+0x21): undefined reference to `luaL_newstate'
main.c:(.text+0x2f): undefined reference to `luaL_openlibs'
main.c:(.text+0x41): undefined reference to `lua_createtable'
main.c:(.text+0x62): undefined reference to `lua_pushnumber'
main.c:(.text+0x82): undefined reference to `lua_pushstring'
main.c:(.text+0x92): undefined reference to `lua_rawset'
main.c:(.text+0xb7): undefined reference to `lua_setfield'
main.c:(.text+0xd5): undefined reference to `luaL_loadbuffer'
main.c:(.text+0xea): undefined reference to `lua_pcall'
main.c:(.text+0xf8): undefined reference to `lua_close'
collect2: error: ld returned 1 exit status
I am working on Linux Debian Stable (updated). Where is the problem and how can this be solved? Thanks for your help.
Since you installed liblua-5.1-dev, I assume you are on Debian or a derivative. There, you have to link with -llua5.1, like this:
gcc -O2 -Wall -I/usr/include/lua5.1 main.c -llua5.1

Compile error using openSSL on Xcode. Undefined symbols for architecture x86_64

I'm trying to execute this function in my code
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stddef.h>
#include <time.h>
#include <math.h>
#include <openssl/rand.h>
unsigned int random_uint(unsigned int limit) {
union {
unsigned int i;
unsigned char c[sizeof(unsigned int)];
} u;
do {
if (!RAND_bytes(u.c, sizeof(u.c))) {
fprintf(stderr, "Can't get random bytes!\n");
exit(1);
}
} while (u.i < (-limit % limit));
return u.i % limit;
}
I have problems with openssl on Xcode, I installed it in the folder /usr/local/ssl/ios/openssl-1.1.0c/, added Library search path and Header but it gives me this error:
Ld Build/Products/Debug/ccc normal x86_64
cd /Users/edoardosavini/Downloads/ccc
export MACOSX_DEPLOYMENT_TARGET=10.12
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cla
ng -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.plat
form/Developer/SDKs/MacOSX10.12.sdk -L/Users/edoardosavini/Downloads/ccc/Build/Products/De
bug -L/usr/local/ssl/ios/openssl-1.1.0c -F/Users/edoardosavini/Downloads/ccc/Build/Product
s/Debug -filelist /Users/edoardosavini/Downloads/ccc/Build/Intermediates/ccc.build/Debug/c
cc.build/Objects-normal/x86_64/ccc.LinkFileList -mmacosx-version-min=10.12 -Xlinker -objec
t_path_lto -Xlinker /Users/edoardosavini/Downloads/ccc/Build/Intermediates/ccc.build/Debug
/ccc.build/Objects-normal/x86_64/ccc_lto.o -Xlinker -export_dynamic -Xlinker -no_deduplica
te /usr/local/ssl/ios/openssl-1.1.0c/libssl.a -lssl -Xlinker -dependency_info -Xlinker /Us
ers/edoardosavini/Downloads/ccc/Build/Intermediates/ccc.build/Debug/ccc.build/Objects-norm
al/x86_64/ccc_dependency_info.dat -o /Users/edoardosavini/Downloads/ccc/Build/Products/Deb
ug/ccc
Undefined symbols for architecture x86_64:
"_RAND_bytes", referenced from:
_random_uint 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)
can you help me please?

std::future and clang with -stdlib=libstdc++

The following program fails to link with clang and -stdlib=libstdc++:
$ cat future.cpp
#include <iostream>
#include <future>
int main()
{
std::future<int> f1 = std::async([](){ return 42; });
f1.wait();
std::cout << "Magic number is: " << f1.get() << std::endl;
}
$ g++-mp-5 future.cpp -std=c++11 && ./a.out
Magic number is: 42
$ clang++-mp=3.5 future.cpp -std=c++11 && ./a.out
Magic number is: 42
When building with clang and -stdlib=libstdc++, the following linking error occurs:
$ clang++-mp-3.5 future.cpp -std=c++11 -stdlib=libstdc++ -I/opt/local/include/gcc5/c++ -I/opt/local/include/gcc5/c++/x86_64-apple-darwin14 -L/opt/local/lib/gcc5 -lstdc++ && ./a.out
Undefined symbols for architecture x86_64:
"std::__once_call", referenced from:
void std::call_once<void (std::__future_base::_State_baseV2::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*>(std::once_flag&, void (std::__future_base::_State_baseV2::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*&&, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*&&, bool*&&) in future-b6480b.o
void std::call_once<void (std::thread::*)(), std::reference_wrapper<std::thread> >(std::once_flag&, void (std::thread::*&&)(), std::reference_wrapper<std::thread>&&) in future-b6480b.o
"std::__once_callable", referenced from:
void std::call_once<void (std::__future_base::_State_baseV2::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*>(std::once_flag&, void (std::__future_base::_State_baseV2::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*&&, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*&&, bool*&&) in future-b6480b.o
void std::__once_call_impl<std::_Bind_simple<std::_Mem_fn<void (std::__future_base::_State_baseV2::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*)> (std::__future_base::_State_baseV2*, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*)> >() in future-b6480b.o
void std::call_once<void (std::thread::*)(), std::reference_wrapper<std::thread> >(std::once_flag&, void (std::thread::*&&)(), std::reference_wrapper<std::thread>&&) in future-b6480b.o
void std::__once_call_impl<std::_Bind_simple<std::_Mem_fn<void (std::thread::*)()> (std::reference_wrapper<std::thread>)> >() in future-b6480b.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
However, a simple program w/o future builds just fine, e.g.:
$ cat simple.cpp
#include <iostream>
#include <future>
int main()
{
std::cout << "Magic number is: " << 42 << std::endl;
}
$ clang++-mp-3.5 simple.cpp -std=c++11 -stdlib=libstdc++ -I/opt/local/include/gcc5/c++ -I/opt/local/include/gcc5/c++/x86_64-apple-darwin14 -L/opt/local/lib/gcc5 -lstdc++ && ./a.out
Magic number is: 42
System is OSX 10.10.4 with macports.
I can't figure out what is the problem. Thanks!
This is an incompatibility between GCC and Clang on Mac OS X.
GCC emits a reference to ___emutls_v._ZSt15__once_callable while clang emits a reference to __ZSt15__once_callable.
Unfortunately, __ZSt15__once_callable and ___emutls_v._ZSt15__once_callable
are not compatible, so doing something like:
asm("__ZSt15__once_callable: jmp ___emutls_v._ZSt15__once_callable");
wouldn't work either.
I also came accross this LLVM bug report: http://lists.cs.uiuc.edu/pipermail/llvmbugs/2014-August/035744.html which probably means clang will never add for support GCC's emutls implementation.
Edit: It looks like support for emutls was added to clang trunk a couple of hours ago in r243438 via -femulated-tls.
Clang on OSX uses an older version of libstdc++. I believe its version 4.2 which does not support many of the C++11 features. If you want to use C++11 with clang on OSX your only choice is libc++.
You could also use gcc-4.x from homebrew which will include a newer version of libstdc++. Unfortunately clang will not easily link with this version of the standard library.
Undefined symbol std::__once_call when building GCC's standard library is explained here https://github.com/msys2/MINGW-packages/issues/5786 and should be fixed when using compiler flag -femulated-tls.

OpenSSL and Rand_bytes

My question is in regards to using OpenSSL on Mac via GCC.
#include <stdio.h>
#include <openssl/rand.h>
int main()
{
unsigned char key[128];
Rand_bytes(key,128);
return 0;
}
I have the following code, that I am trying to compile with GCC. Here is what I enter into the command line
gcc -o ossl ossl.c -lcrypto -lssl
However I get the following error.
Undefined symbols for architecture x86_64:
"_Rand_bytes", referenced from:
_main in cc2hf0Ij.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
I am not experienced when it comes to using openssl. Why am I receiving Undefined symbols for architecture x86_64?
int main()
{
unsigned char key[128];
Rand_bytes(key,128);
return 0;
}
Try RAND_bytes:
int main()
{
unsigned char key[128];
int rc = RAND_bytes(key,sizeof(key));
if(rc != 1)
/* Handle failure */
...
OPENSSL_cleanse(key,sizeof(key));
return 0;
}
The OpenSSL docs are at RAND_bytes(3).

C++ Link Error Compiling With Macports gcc47

I am writing a small app on Mac 10.7.5 with gcc47 via macports in Eclipse CDT to learn the new features in C++11. I have a large amount of code compiling, linking and running. When I add the call to "async" I get a linker error. The most simple program that will reproduce the link error follows. Thanks in advance for any help.
#include <iostream>
#include <future>
using namespace std;
void bar()
{
cout << "!!!Hello World!!!" << endl;
}
int main() {
future<void> f1(async(bar));
return 0;
}
The output is:
21:56:46 **** Incremental Build of configuration Debug for project Hello ****
make all
Building file: ../src/Hello.cpp
Invoking: GCC C++ Compiler g++ -D__GXX_EXPERIMENTAL_CXX0X__ -I/opt/local/include/gcc47/c++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"src/Hello.d" -MT"src/Hello.d" -o "src/Hello.o" "../src/Hello.cpp"
Finished building: ../src/Hello.cpp
Building target: Hello
Invoking: MacOS X C++ Linker g++ -o "Hello"
./src/Hello.o Undefined symbols for architecture x86_64:
"___emutls_v._ZSt11__once_call", referenced from:
void std::call_once<void (std::__future_base::_State_base::*)(std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()>&, bool&),
std::__future_base::_State_base* const,
std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()> >,
std::reference_wrapper<bool> >(std::once_flag&, void
(std::__future_base::_State_base::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()>&, bool&),
std::__future_base::_State_base* const&&,
std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()> >&&,
std::reference_wrapper<bool>&&) in Hello.o
void std::call_once<void (std::thread::*)(), std::reference_wrapper<std::thread> >(std::once_flag&, void
(std::thread::*&&)(), std::reference_wrapper<std::thread>&&) in
Hello.o "___emutls_v._ZSt15__once_callable", referenced from:
void std::call_once<void (std::__future_base::_State_base::*)(std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()>&, bool&),
std::__future_base::_State_base* const,
std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()> >,
std::reference_wrapper<bool> >(std::once_flag&, void
(std::__future_base::_State_base::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()>&, bool&),
std::__future_base::_State_base* const&&,
std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()> >&&,
std::reference_wrapper<bool>&&) in Hello.o
void std::call_once<void (std::thread::*)(), std::reference_wrapper<std::thread> >(std::once_flag&, void
(std::thread::*&&)(), std::reference_wrapper<std::thread>&&) in
Hello.o
void std::__once_call_impl<std::_Bind_simple<std::_Mem_fn<void (std::__future_base::_State_base::*)(std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()>&, bool&)>
(std::__future_base::_State_base*,
std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base,
std::__future_base::_Result_base::_Deleter> ()> >,
std::reference_wrapper<bool>)> >() in Hello.o
void std::__once_call_impl<std::_Bind_simple<std::_Mem_fn<void (std::thread::*)()> (std::reference_wrapper<std::thread>)> >() in
Hello.o ld: symbol(s) not found for architecture x86_64 collect2:
error: ld returned 1 exit status make: *** [Hello] Error 1
The information at http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54806 and https://trac.macports.org/ticket/36093 suggests that this error is due to a bug in the MacPorts version of GCC. According to https://trac.macports.org/ticket/36093#comment:35 this has been fixed as of r98493 of MacPorts. I would expect that your problem would go away if you updated to that revision of the port repository and then reinstalled GCC.

Resources