Can I use init_ui() in architecture x86_64? - c

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

Related

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 Cilk plus in GCC5.2.0

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.

fatal error: 'X11/Xlib.h' file not found

I have installed XQuartz.
I compiled using g++:
g++ -o -lX11 -I/opt/X11/include window2.cc
Error
Undefined symbols for architecture x86_64:
"_XCreateWindow", referenced from:
_main in window2-dXb9bZ.o
"_XFlush", referenced from:
_main in window2-dXb9bZ.o
"_XMapWindow", referenced from:
_main in window2-dXb9bZ.o
"_XOpenDisplay", referenced from:
_main in window2-dXb9bZ.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
If i compile like this:
g++ window2.cc -o window -lX11 -I/opt/X11/include
Error
ld: library not found for -lX11
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Im sure Xlib.h is in /opt/X11/include
Code:
#include <X11/Xlib.h>
#include <unistd.h>
int main()
{
// Open a display.
Display *d = XOpenDisplay(0);
if ( d )
{
// Create the window
Window w = XCreateWindow(d, DefaultRootWindow(d), 0, 0, 200,
100, 0, CopyFromParent, CopyFromParent,
CopyFromParent, 0, 0);
// Show the window
XMapWindow(d, w);
XFlush(d);
// Sleep long enough to see the window.
sleep(10);
}
return 0;
}
How do I solve this problem ? Thanks in advance
Problem resolved. In case anyone who's interested:
You have to compile like this:
g++ -o window window.cc -I/usr/X11R6/include -L/usr/X11R6/lib -lX11
Try
cc -I /opt/X11/include/ test.c -L /opt/X11/lib -lX11

compiling a program that includes libmodbus in C

I am a newbie in C... I wrote a very simple modbus1.c that includes libmodbus (whose source I downloaded, unzipped, untarred, ./configure'd, make'd and make install'd successfully).
When I try to make modbus1.c I get this:
cc -Wall -g modbus1.c -o modbus1
Undefined symbols for architecture x86_64:
"_modbus_close", referenced from:
_main in modbus1-6cd135.o
"_modbus_connect", referenced from:
_main in modbus1-6cd135.o
"_modbus_free", referenced from:
_main in modbus1-6cd135.o
"_modbus_new_tcp_pi", referenced from:
_main in modbus1-6cd135.o
"_modbus_read_bits", referenced from:
_main in modbus1-6cd135.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: *** [modbus1] Error 1
I am running OSX snow leopard and have successfully used make to compile small programs before (tutorial level programs...) Here is the modbus1.c I am trying to compile:
#include <stdio.h>
#include <stdlib.h>
#include <modbus.h>
int main(int argc, char *argv[]){
modbus_t *plc_client;
plc_client = modbus_new_tcp_pi("192.168.1.230","502");
if (plc_client == NULL) {
fprintf(stderr, "Unable to allocate libmodbus context\n");
return -1;
}
if (modbus_connect(plc_client) == -1) {
fprintf(stderr, "Connection failed: \n");
modbus_free(plc_client);
return -1;
}
else if(modbus_connect(plc_client) == 0) {
printf("MODBUS CONNECTION SUCCESSFUL\n");
}
uint8_t* catcher = malloc(sizeof(uint8_t));
if(modbus_read_bits(plc_client, 2000, 1, catcher)>0){
printf("READ SUCCESSFUL");
}
else{
printf("READ FAILED");
}
free(catcher);
modbus_close(plc_client);
modbus_free(plc_client);
return 0;
}
Any help will be greatly appreciated! Thanks!
-Niko
Try this
cc -Wall -g modbus1.c -o modbus1 -L/path/to/libmodbus -lmodbus
You should replace that /path/to/libmodbus with the actual path of directory that includes the libmodbus.dylib in your system.

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