2013年4月7日 星期日

gcc引用非標準函式庫(gcc -l -L)

gcc引用非標準函式庫要求正確的引用順序
格式:
gcc [source code: .c, .o, .a] [ -l | -L ]
Example:
gcc sin.c -lm -L/usr/lib -I/usr/include
 
 
Try:
gcc $(CFLAGS) -o xxx xxx.o ../../dbus/libdbus.a  = O OK 
gcc $(CFLAGS) -ldbus -L../../dbus -o xxx xxx.o  = X FAIL
gcc $(CFLAGS) -ldbus -L../../dbus -o xxx xxx.o  = X FAIL
gcc $(CFLAGS) -o xxx xxx.o -ldbus -L../../dbus  = O OK 
gcc $(CFLAGS) -o xxx xxx.o -L../../dbus  = X FAIL
gcc $(CFLAGS) -o xxx xxx.o -L../../dbus -ldbus  = O OK  

gcc -I、:=與#include

http://www.rapidtables.com/code/linux/gcc/gcc-i.htm

gcc -I option flag

gcc -I adds include directory of header files.
Syntax
$ gcc -Idir [options] [source files] [object files] [-o output file]
Example
proj/src/myheader.h:

// myfile.h
#define NUM1 5



myfile.c:

// myfile.c
#include
#include "myheader.h"

void main()
{
int num = NUM1;
printf("num=%d\n", num);
}



Build myfile.c without include directory proj/src :

$ gcc myfile.c -o myfile
myfile.c:2:22: fatal error: myheader.h: No such file or directory
compilation terminated.
$



Build myfile.c with include directory proj/src :

$ gcc -Iproj/src myfile.c -o myfile
$ ./myfile
num=5
$


這個方法是可行的,之前的編譯include路徑無效問題出現在
GCC=gcc
LIBFLAGS:=$(CFLAGS) -c
CFLAGS:= -Dxxx -Wall -g -I...
LIBFLAGS使用當下的選項,CFLAGS為空值,從下make指令後可看出。
應該改為:
GCC=gcc
LIBFLAGS=$(CFLAGS) -c
CFLAGS:= -Dxxx -Wall -g -I...

2013年4月3日 星期三

linker input file unused because linking not done

gcc -c

正當我被GCC "undefined reference"搞得死去活來時...

(1)
拜讀了這篇精闢的解說....
http://ticktick.blog.51cto.com/823160/431329
(2)
undefined reference還是除不完
發現undefined reference to android_atomic_release_cas

extern inline int android_atomic_release_cas(int32_t old_value,
int32_t new_value,
volatile int32_t *ptr)
{
android_memory_barrier();
return android_atomic_cas(old_value, new_value, ptr);
}

改用g++編譯。
(3)
g++編譯錯誤, invalid conversion from...
解决方法:CXXFLAGS = -fpermissive

2013年4月2日 星期二

Android Makefile

http://huenlil.pixnet.net/blog/post/23801824-%5B%E8%BD%89%5Dandroid-jni%E5%AF%A6%E4%BE%8B
http://3sec.kilab.tw/?p=244

GCC #error

#error 強迫compiler停止compile,主要是用來除錯用的。與ifdef並用,可以讓程式在某些define
錯誤狀況下中止

語法是
#error 錯誤訊息

當程式遇到#error,錯誤訊息會在console端顯示出來,一般會和其他compiler所定義的訊息
一起顯示出來。

找不到struct ucred

http://linux.die.net/man/7/unix
Ancillary Messages

SCM_CREDENTIALS
Send or receive UNIX credentials. This can be used for authentication. The credentials are passed as a struct ucred ancillary
message. Thus structure is defined in as follows:

struct ucred {
pid_t pid; /* process ID of the sending process */
uid_t uid; /* user ID of the sending process */
gid_t gid; /* group ID of the sending process */
};

Since glibc 2.8, the _GNU_SOURCE feature test macro must be defined (before including any header files) in order to obtain the
definition of this structure.
The credentials which the sender specifies are checked by the kernel. A process with effective user ID 0 is allowed to specify values
that do not match its own. The sender must specify its own process ID (unless it has the capability CAP_SYS_ADMIN), its user
ID, effective user ID, or saved set-user-ID (unless it has CAP_SETUID), and its group ID, effective group ID, or saved
set-group-ID (unless it has CAP_SETGID). To receive a struct ucred message the SO_PASSCRED option must be enabled on the socket.