How to link Scintilla? - c

I downloaded bait from Scintilla's documentation page but whenever I try to compile it using the provided makefile, it fails with this error:
$ make
gcc `pkg-config --cflags gtk+-2.0` -I../scintilla/include -DGTK -DSCI_LEXER -W -Wall -c bait.c -o bait.o
gcc -DGTK bait.o ../scintilla/gtk/LexA68k.o ../scintilla/gtk/LexAPDL.o ../scintilla/gtk/LexASY.o ../scintilla/gtk/LexAU3.o ../scintilla/gtk/LexAVE.o ../scintilla/gtk/LexAVS.o ../scintilla/gtk/LexAbaqus.o ../scintilla/gtk/LexAda.o ../scintilla/gtk/LexAsm.o ../scintilla/gtk/LexAsn1.o ../scintilla/gtk/LexBaan.o ../scintilla/gtk/LexBash.o ../scintilla/gtk/LexBasic.o ../scintilla/gtk/LexBullant.o ../scintilla/gtk/LexCLW.o ../scintilla/gtk/LexCOBOL.o ../scintilla/gtk/LexCPP.o ../scintilla/gtk/LexCSS.o ../scintilla/gtk/LexCaml.o ../scintilla/gtk/LexCmake.o ../scintilla/gtk/LexCoffeeScript.o ../scintilla/gtk/LexConf.o ../scintilla/gtk/LexCrontab.o ../scintilla/gtk/LexCsound.o ../scintilla/gtk/LexD.o ../scintilla/gtk/LexDMAP.o ../scintilla/gtk/LexDMIS.o ../scintilla/gtk/LexECL.o ../scintilla/gtk/LexEScript.o ../scintilla/gtk/LexEiffel.o ../scintilla/gtk/LexErlang.o ../scintilla/gtk/LexFlagship.o ../scintilla/gtk/LexForth.o ../scintilla/gtk/LexFortran.o ../scintilla/gtk/LexGAP.o ../scintilla/gtk/LexGui4Cli.o ../scintilla/gtk/LexHTML.o ../scintilla/gtk/LexHaskell.o ../scintilla/gtk/LexInno.o ../scintilla/gtk/LexKVIrc.o ../scintilla/gtk/LexKix.o ../scintilla/gtk/LexLaTeX.o ../scintilla/gtk/LexLisp.o ../scintilla/gtk/LexLout.o ../scintilla/gtk/LexLua.o ../scintilla/gtk/LexMMIXAL.o ../scintilla/gtk/LexMPT.o ../scintilla/gtk/LexMSSQL.o ../scintilla/gtk/LexMagik.o ../scintilla/gtk/LexMarkdown.o ../scintilla/gtk/LexMatlab.o ../scintilla/gtk/LexMetapost.o ../scintilla/gtk/LexModula.o ../scintilla/gtk/LexMySQL.o ../scintilla/gtk/LexNimrod.o ../scintilla/gtk/LexNsis.o ../scintilla/gtk/LexOScript.o ../scintilla/gtk/LexOpal.o ../scintilla/gtk/LexOthers.o ../scintilla/gtk/LexPB.o ../scintilla/gtk/LexPLM.o ../scintilla/gtk/LexPO.o ../scintilla/gtk/LexPOV.o ../scintilla/gtk/LexPS.o ../scintilla/gtk/LexPascal.o ../scintilla/gtk/LexPerl.o ../scintilla/gtk/LexPowerPro.o ../scintilla/gtk/LexPowerShell.o ../scintilla/gtk/LexProgress.o ../scintilla/gtk/LexPython.o ../scintilla/gtk/LexR.o ../scintilla/gtk/LexRebol.o ../scintilla/gtk/LexRuby.o ../scintilla/gtk/LexRust.o ../scintilla/gtk/LexSML.o ../scintilla/gtk/LexSQL.o ../scintilla/gtk/LexSTTXT.o ../scintilla/gtk/LexScriptol.o ../scintilla/gtk/LexSmalltalk.o ../scintilla/gtk/LexSorcus.o ../scintilla/gtk/LexSpecman.o ../scintilla/gtk/LexSpice.o ../scintilla/gtk/LexTACL.o ../scintilla/gtk/LexTADS3.o ../scintilla/gtk/LexTAL.o ../scintilla/gtk/LexTCL.o ../scintilla/gtk/LexTCMD.o ../scintilla/gtk/LexTeX.o ../scintilla/gtk/LexTxt2tags.o ../scintilla/gtk/LexVB.o ../scintilla/gtk/LexVHDL.o ../scintilla/gtk/LexVerilog.o ../scintilla/gtk/LexVisualProlog.o ../scintilla/gtk/LexYAML.o ../scintilla/gtk/LexerBase.o ../scintilla/gtk/LexerModule.o ../scintilla/gtk/LexerSimple.o ../scintilla/bin/scintilla.a -o bait -lstdc++ `pkg-config --libs gtk+-2.0 gthread-2.0`
Undefined symbols for architecture x86_64:
"_g_module_close", referenced from:
DynamicLibraryImpl::~DynamicLibraryImpl() in scintilla.a(PlatGTK.o)
"_g_module_open", referenced from:
DynamicLibrary::Load(char const*) in scintilla.a(PlatGTK.o)
"_g_module_symbol", referenced from:
DynamicLibraryImpl::FindFunction(char const*) in scintilla.a(PlatGTK.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: *** [bait] Error 1
There is a ../scintilla/include directory with the Scintilla header files in it.

Try adding -lgmodule to cflags

Related

How to use extern symbols in shared library in C

I am trying to compile following test files to create a shared library:
answer.c
#include <stdio.h>
#include "add.h"
extern int myvar();
int answer()
{
printf("\r\n myvar:%d \r\n", myvar());
setSummand(20);
return add(22); // Will return 42 (=20+22)
}
add.c
#include <stdio.h>
int gSummand;
void setSummand(int summand)
{
printf("1Library is initialized\n");
gSummand = summand;
}
int add(int summand)
{
return gSummand + summand;
}
I want to create a shared library from the 2 files "answer.c" "add.c", I am using following commands:
gcc -c answer.c -o answer.o
gcc -c add.c -o add.o
gcc -shared add.o answer.o -o libtest.so
However third command gives following error:
answer.o:answer.c:(.text+0x9): undefined reference to `myvar'
answer.o:answer.c:(.text+0x9): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `myvar'
collect2: error: ld returned 1 exit status
Same setup links successfully if I use the following command to create static library instead of dynamic. Therefore, I don't see any error if I try following command:
ar rcs libtest.a add.o answer.o
Want to know if I am missing here something. Also want to know how to use external symbols in shared library.
ELF shared libraries need to be position independent. They will be mapped into the executable's address space at an address that isn't known until run time. This means no absolute address call instructions, such as might be used to call myvar().
You need to specify -fpic when you compile the source into object files, when those object files will be placed into a shared library. This tells the compiler to generate code that does not use absolute addresses, etc. so it can be position independent.
Example main.c file to use this library:
extern int answer(void);
int myvar() { return 1; }
int main(void) { return answer(); }
Example without -fpic:
[test]$ gcc -c add.c
[test]$ gcc -c answer.c
[test]$ gcc -shared add.o answer.o -o libtest.so
/usr/bin/ld: add.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: answer.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
Example with -fpic:
[test]$ gcc -fpic -c add.c
[test]$ gcc -fpic -c answer.c
[test]$ gcc -shared add.o answer.o -o libtest.so
[test]$ gcc main.c libtest.so
[test]$ LD_LIBRARY_PATH=. ./a.out
myvar:1
1Library is initialized

failure linking C code

I a ma total noob in C programming. I took some code that throws this error when I run make :
Undefined symbols for architecture x86_64:
"_rp_osc_adc_sign", referenced from:
_rp_osc_meas_min_max in worker.o
_meas_period in worker.o
"_rp_osc_meas_cnv_cnt", referenced from:
_rp_osc_meas_convert in worker.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: *** [make_c_app] Error
I am using OS X Yosemite and here's the make file
CC=$(CROSS_COMPILE)gcc
RM=rm
OBJECTS=main.o fpga.o worker.o calib.o fpga_awg.o generate.o fpga_pid.o pid.o
INCLUDE=
#CFLAGS=$(CFLAGS) -Wall -Werror -g
CFLAGS+= -Wall -Werror -g -fPIC $(INCLUDE)
LDFLAGS=-shared
OUT_DIR=../
C_OUT_NAME=$(OUT_DIR)controller.so
all: make_c_app
clean: clean_c_app
make_c_app: $(OBJECTS)
$(CC) -o $(C_OUT_NAME) $(OBJECTS) $(CFLAGS) $(LDFLAGS)
clean_c_app:
$(RM) -f $(C_OUT_NAME) $(OBJECTS)
and the method the linker is complaining about
int rp_osc_meas_min_max(rp_osc_meas_res_t *ch_meas, int sig_data)
{
int s_data = rp_osc_adc_sign(sig_data);
if(ch_meas->min > s_data)
ch_meas->min = s_data;
if(ch_meas->max < s_data)
ch_meas->max = s_data;
ch_meas->avg += s_data;
return 0;
}
Where the error can come from ?
EDIT : rp_osc_adc_sign is defined the following way
inline int rp_osc_adc_sign(int in_data)
{
int s_data = in_data;
if(s_data & (1<<(c_osc_fpga_adc_bits-1)))
s_data = -1 * ((s_data ^ ((1<<c_osc_fpga_adc_bits)-1)) + 1);
return s_data;
}
You should delete the inline tag that is written before the function
rp_osc_adc_sign(int in_data)
rp_osc_meas_cnv_cnt

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

Undefined symbol _main, however I do have a main function?

Here is my console output:
Tylers-MacBook-Pro:laser_finder_c tylerjw$ make
gcc -g -Wall -c -o dots_img.o dots_img.c
gcc -g -Wall -c -o no_dots_img.o no_dots_img.c
gcc -g -Wall -c -o point.o point.c
gcc -g -Wall -c -o images.o images.c
gcc -g -Wall -c -o laser_finder_c.o laser_finder_c.c
gcc dots_img.o no_dots_img.o point.o images.o laser_finder_c.o -g -Wall -o laser_finder_c
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [laser_finder_c] Error 1
This doesn't seem to make any sense as the main function is defined in laser_finder_c.c. Below is my makefile. I'm real confused as to why this is happening.
TARGET = laser_finder_c
OBJECTS = dots_img.o no_dots_img.o point.o images.o laser_finder_c.o
#######################################################################################
CFLAGS = -g -Wall
ASFLAGS = -Wall
LDFLAGS = -g -Wall
CC = gcc
AS = gcc
########################################################################################
$(TARGET): $(OBJECTS)
$(CC) $(OBJECTS) $(LDFLAGS) -o $(TARGET)
point.o: point.h
images.o: images.h
laser_finder_c.o: images.h point.h
dots_img.o: images.h
no_dots_img.o: images.h
clean:
rm -f $(OBJECTS) $(TARGET)
Here is the definition of main in laser_finder_c.c
// laser_finder.c - finds the location of laser points comparing two images
#include "point.h"
#include <stdio.h>
#include <stdlib.h>
// #defines ... removed
int main()
{
// ... code removed
return 0;
}
For context the output of gcc -v is:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.38) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix

Cannot compile against gobject-2.0

I am trying to learn the gobject system, so I read some of the documentation on the gnome site and made a simple gobject parented to GObject. I don't wan't to keep running the gcc job every time I want to compile, and I also wanted to learn a build system at the same time. I tried autotools, which I got working to the point where pkg-config searches for gobject-2.0.
To compile my project from the command line, I do: gcc *.c $(pkg-config --cflags --libs gobject-2.0) (I had to take out the extra ticks for formatting). Anyway <--- this command works.
However: gcc $(pkg-config --cflags --libs gobject-2.0) *.c, which SHOULD BE the SAME COMMAND, returns this:
main.c: In function 'main':
main.c:10:10: warning: initialization from incompatible pointer type [enabled by default]
/tmp/ccxO7wkX.o: In function `main':
main.c:(.text+0x1a): undefined reference to `g_type_init'
main.c:(.text+0x27): undefined reference to `g_type_create_instance'
/tmp/ccPu2beU.o: In function `a_get_type':
myobject.c:(.text+0x57): undefined reference to `g_type_register_static'
collect2: ld returned 1 exit status
Yes, it is the EXACT SAME COMMAND WITH ANOTHER ORDER. I have no clue what's going wrong. Here is how autotools/automake does the build:
gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -lgobject-2.0 -lglib-2.0 -o GObjectTest main.o myobject.o
main.o: In function `main':
/home/aj/Desktop/GObjectTest/src/main.c:9: undefined reference to `g_type_init'
/home/aj/Desktop/GObjectTest/src/main.c:10: undefined reference to `g_type_create_instance'
myobject.o: In function `a_get_type':
/home/aj/Desktop/GObjectTest/src/myobject.c:19: undefined reference to `g_type_register_static'
collect2: ld returned 1 exit status
make: *** [GObjectTest] Error 1
Is this some quirky gcc compile thing? I tried with clang too no avail, which makes me think that this is a linker problem? I really have no idea, and was hoping that someone out there will. This is pretty frustrating.
Here is the "offending code":
GType a_get_type (void)
{
if (type == 0) {
GTypeInfo info = {
sizeof(AClass),
NULL,
NULL,
(GClassInitFunc) a_class_init,
NULL,
NULL,
sizeof(A),
0,
(GInstanceInitFunc) NULL
};
type = g_type_register_static (G_TYPE_OBJECT,
"AType",
&info, 0);
}
return type;
}
Edit: Here is the output of my autotools build, the make part anyway:
$ make
make all-recursive
make[1]: Entering directory `/home/aj/Desktop/GObjectTest'
Making all in src
make[2]: Entering directory `/home/aj/Desktop/GObjectTest/src'
gcc -DHAVE_CONFIG_H -I. -I.. -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
main.c: In function 'main':
main.c:10:10: warning: initialization from incompatible pointer type [enabled by default]
mv -f .deps/main.Tpo .deps/main.Po
gcc -DHAVE_CONFIG_H -I. -I.. -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -MT myobject.o -MD -MP -MF .deps/myobject.Tpo -c -o myobject.o myobject.c
mv -f .deps/myobject.Tpo .deps/myobject.Po
gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -lgobject-2.0 -lglib-2.0 -o GObjectTest main.o myobject.o
main.o: In function `main':
/home/aj/Desktop/GObjectTest/src/main.c:9: undefined reference to `g_type_init'
/home/aj/Desktop/GObjectTest/src/main.c:10: undefined reference to `g_type_create_instance'
myobject.o: In function `a_get_type':
/home/aj/Desktop/GObjectTest/src/myobject.c:19: undefined reference to `g_type_register_static'
collect2: ld returned 1 exit status
make[2]: *** [GObjectTest] Error 1
make[2]: Leaving directory `/home/aj/Desktop/GObjectTest/src'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/home/aj/Desktop/GObjectTest'
make: *** [all] Error 2
Here is the interesting part: replace gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -lgobject-2.0 -lglib-2.0 -o GObjectTest main.o myobject.o with gcc -o GObjectTest main.o myobject.o -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -lgobject-2.0 -lglib-2.0 and it works.

Resources