Compile Cilk plus in GCC5.2.0 - c

Does anybody know how to compile the following code with Cilk plus in gcc5.2.0 correctly? With gcc -fcilkplus * or g++, I always get errors.
#include <cilk/cilk.h>
#include <assert.h>
int fib(int n) {
if (n < 2)
return n;
int a = cilk_spawn fib(n-1);
int b = fib(n-2);
cilk_sync;
return a + b;
}
int main() {
int result = fib(30);
assert(result == 832040);
return 0;
}
result:
Undefined symbols for architecture x86_64:
"___cilkrts_enter_frame_1", referenced from:
fib(int) in ccY1qrGL.o
"___cilkrts_enter_frame_fast_1", referenced from:
__cilk_spn_0 in ccY1qrGL.o
"___cilkrts_leave_frame", referenced from:
fib(int) in ccY1qrGL.o
__cilk_spn_0 in ccY1qrGL.o
"___cilkrts_rethrow", referenced from:
fib(int) in ccY1qrGL.o
"___cilkrts_save_fp_ctrl_state", referenced from:
fib(int) in ccY1qrGL.o
"___cilkrts_sync", referenced from:
fib(int) in ccY1qrGL.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
There are very few topics about this online. thanks

GCC is configured to add -lcilkrts by default to its linker command when -fcilkplus is given by users, but this default behavior can be overridden if there is any platform-specific configuration. I think that is happening on OS X, but it needs to be fixed in my opinion. Anyway, it seems that there is no short-term solution other than adding -lcilkrts as suggested above.

Related

Undefined symbols for architecture x86_64 termcap.h in C

I have this code:
#include <stdio.h>
#include <termcap.h>
#include <stdlib.h>
int main() {
char *termtype = getenv("TERM");
int li = tgetnum("li");
printf("%d", li);
return 0;
}
I can't compile it because I get
Undefined symbols for architecture x86_64:
"_tgetnum", referenced from:
_main in main-11560e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Why is it happening?
MacOS Catalina

Can I use init_ui() in architecture x86_64?

I am learning for fun with building my own matrix and im on my Mac is the only computer I have and I dont want to go through Virtual environment just to play around with matrix on Mac. Is there a way or no is what I will like to find out.
int
main(int argc, char **argv) {
if (!init_ui()) {
return EXIT_FAILURE;
}
matrix_init();
for (int i = 0; i < ITERATIONS; i++) {
matrix_update();
show_matrix();
usleep(REFRESH_DELAY);
}
return EXIT_SUCCESS;
}
Here is the init_ui() function
bool init_ui() {
//set the matrix all to black
//move this later to matrix.c
for (int x = 0; x < MAXX; x++) {
for (int y = 0; y < MAXY; y++) {
matrix[x][y].char_value = 0;
matrix[x][y].char_value = 0;
}
}
//init curses
uiwindow = initscr();
if (uiwindow == NULL) return false;
start_color();
if (!has_colors() || !can_change_color() || COLOR_PAIRS < 6) {
printf("Warning. Your terminal can't handle this program.\n");
return false;
}
set_colors();
return true;
}//end bool
Error :
clang -Wall -g main.o matrix.o ui.o -o mainapp
Undefined symbols for architecture x86_64:
"_COLOR_PAIRS", referenced from:
_init_ui in ui.o
"_can_change_color", referenced from:
_init_ui in ui.o
"_delwin", referenced from:
_cleanup_ui in ui.o
"_endwin", referenced from:
_cleanup_ui in ui.o
"_has_colors", referenced from:
_init_ui in ui.o
"_init_color", referenced from:
_set_colors in ui.o
"_init_pair", referenced from:
_set_colors in ui.o
"_initscr", referenced from:
_init_ui in ui.o
"_start_color", referenced from:
_init_ui in ui.o
"_stdscr", referenced from:
_cleanup_ui in ui.o
_show_matrix in ui.o
"_waddch", referenced from:
_show_matrix in ui.o
"_wcolor_set", referenced from:
_show_matrix in ui.o
"_wmove", referenced from:
_show_matrix in ui.o
"_wrefresh", referenced from:
_cleanup_ui in ui.o
_show_matrix in ui.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mainapp] Error 1
The init_ui() is not the only the problem here. I mean I fell in love with c so I can't stop coding in c even though I mastered python but c just took my attention. So idk I would love to know more than just write boring code
So to fix this error. The only thing needed was -lncurses which is explained # Error while compiling ncurses app on Mac OS X
In my scenario I needed to compile as according shown below
mainapp: main.o matrix.o ui.o
$(CC) $(CFLAGS) main.o matrix.o ui.o -o mainapp -lncurses
Thank you #dratenik && #kaylum

Undefined symbols for architecture x86_64: "_swap", referenced from: _main in swap-315897.o ld: symbol(s) not found for architecture x86_64

I'm trying to reproduce the code of the book Effective C: An Introduction to Professional C Programming by R. Seacord
#include <stdio.h>
void swap(int, int); // defined in Listing 2-2
int main(void) {
int a = 21;
int b = 17;
swap(a, b);
printf("main: a = %d, b = %d\n", a, b);
return 0;
}
When I compile the code on my MacOS 10.15.7 with gcc swap.c, I get the following error:
Undefined symbols for architecture x86_64:
"_swap", referenced from:
_main in swap-315897.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
What shall I do to correct it?

Error compiling allegro5 program using gcc on osx

I am trying to compile a simple allegro5 program on Mac OSX 10.12 but am getting an undefined symbols error. Here is the command I ran in the terminal
gcc main.c -o hello -I/usr/local/include/ -L/usr/local/lib -lallegro_main
And here is my code.
#include <stdio.h>
#include <allegro5/allegro.h>
int main(int argc, char **argv)
{
ALLEGRO_DISPLAY *display = NULL;
if(!al_init())
{
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
display = al_create_display(640, 480);
if(!display)
{
fprintf(stderr, "failed to create display!\n");
return -1;
}
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
al_rest(10.0);
al_destroy_display(display);
return 0;
}
Here is the error I get
Undefined symbols for architecture x86_64:
"_al_clear_to_color", referenced from:
__al_mangled_main in main-b86b99.o
"_al_create_display", referenced from:
__al_mangled_main in main-b86b99.o
"_al_destroy_display", referenced from:
__al_mangled_main in main-b86b99.o
"_al_flip_display", referenced from:
__al_mangled_main in main-b86b99.o
"_al_install_system", referenced from:
__al_mangled_main in main-b86b99.o
"_al_map_rgb", referenced from:
__al_mangled_main in main-b86b99.o
"_al_rest", referenced from:
__al_mangled_main in main-b86b99.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Is it possible that I did not install allegro correctly? I installed it using homebrew according to the allegro wiki instructions. https://wiki.allegro.cc/index.php?title=Getting_Started#Mac_OS
Those are linker errors. You need to link to lallegro.

Compile embedded lua in C

Hey everyone I found this code that embeds Lua in C and I cannot figure out how to get GCC to compile it. I have Lua installed, but how do I link the Lua libraries?
Here is the code I found:
#include <stdio.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
/* lua interpreter */
lua_State* l;
int main () {
int dofile;
/* initialize lua */
l = lua_open();
/* load lua libraries */
luaL_openlibs(l);
/* run the hello.lua script */
dofile = luaL_dofile(l, "hello.lua");
if (dofile == 0) {
/* call foo */
lua_getglobal(l,"foo");
lua_call(l,0,0);
}
else {
printf("Error, unable to run hello.lua\n");
}
/* cleanup Lua */
lua_close(l);
return 0;
}
How do I get this to compile?
I am trying this command to compile
gcc -o embed_hello -L/users/etrosclair/Downloads/lua-5.1.4 -I/users/etrosclair/Downloads/lua-5.1.4 luaTest.c
Here is the error:
Undefined symbols for architecture x86_64:
"_luaL_newstate", referenced from:
_main in ccF0995Q.o
"_luaL_openlibs", referenced from:
_main in ccF0995Q.o
"_luaL_loadfile", referenced from:
_main in ccF0995Q.o
"_lua_pcall", referenced from:
_main in ccF0995Q.o
"_lua_getfield", referenced from:
_main in ccF0995Q.o
"_lua_call", referenced from:
_main in ccF0995Q.o
"_lua_close", referenced from:
_main in ccF0995Q.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
All the lua libraries and headers are in the lua-5.1.4 folder the .o files are also in there too.
Thanks
Thanks
Depends if you want it statically or dynamically compiled.
For static, add -llua (or lua5.1 or lua51; depending on your setup)

Resources