最近在做語音識別相關,發現這個領域大部分代碼都是C++和C都混編都工程,研究了下混編方式,寫下記錄過程
cust@virtualbox:~/alsa_test$ ls
alsa libasound.so libasound.so.2 libasound.so.2.0.0 main.cpp Makefile test_one.c test_one.h
main文件使用cpp結尾,test_one採用c結尾,如何混編呢?很簡單,增加頭文件,並在頭文件裏用一個宏!!
test_one.h內容
#ifndef __TEST_ONE_H__
#define __TEST_ONE_H__
#ifdef __cplusplus
extern "C"
{
#endif
extern int test_one();
#ifdef __cplusplus
}
#endif
#endif
main.cpp內容
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "alsa/asoundlib.h"
#include "test_one.h"
int main(int argc, char *argv[])
{
test_one();
}
Makefile內容
PROJECT_DIR := $(shell pwd)
TARGET = test
CC := /mcuzone/work/buildroot/output/rockchip_rk3308_release/host/usr/bin/aarch64-rockchip-linux-gnu-gcc
CXX := /mcuzone/work/buildroot/output/rockchip_rk3308_release/host/usr/bin/aarch64-rockchip-linux-gnu-g++
LD := /mcuzone/work/buildroot/output/rockchip_rk3308_release/host/usr/bin/aarch64-rockchip-linux-gnu-ld
LIBS := -ldl -lpthread -lasound
DEFINES := -fpic
INCLUDE := -I$(PROJECT_DIR)
CFLAGS := -g -Wall -O3 $(DEFINES) $(INCLUDE)
CXXFLAGS:= $(CFLAGS)
LDFLAGS := -L$(PROJECT_DIR)
# source file
SOURCE := $(wildcard ./*.c) $(wildcard ./*.cpp)
OBJS := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE)))
$(TARGET) : $(OBJS)
$(CXX) $(CXXFLAGS) -o $@ $(OBJS) $(LDFLAGS) $(LIBS)
.PHONY : clean
all : $(TARGET)
clean :
rm -rf ./*.o
rm $(TARGET)
test_one.c內容
#include <alsa/asoundlib.h>
int test_one() {
int val;
printf("ALSA library version: %s\n",
SND_LIB_VERSION_STR);
printf("\nPCM stream types:\n");
for (val = 0; val <= SND_PCM_STREAM_LAST; val++)
printf(" %s\n",
snd_pcm_stream_name((snd_pcm_stream_t)val));
printf("\nPCM access types:\n");
for (val = 0; val <= SND_PCM_ACCESS_LAST; val++)
printf(" %s\n",
snd_pcm_access_name((snd_pcm_access_t)val));
printf("\nPCM formats:\n");
for (val = 0; val <= SND_PCM_FORMAT_LAST; val++)
if (snd_pcm_format_name((snd_pcm_format_t)val)
!= NULL)
printf(" %s (%s)\n",
snd_pcm_format_name((snd_pcm_format_t)val),
snd_pcm_format_description(
(snd_pcm_format_t)val));
printf("\nPCM subformats:\n");
for (val = 0; val <= SND_PCM_SUBFORMAT_LAST;
val++)
printf(" %s (%s)\n",
snd_pcm_subformat_name((
snd_pcm_subformat_t)val),
snd_pcm_subformat_description((
snd_pcm_subformat_t)val));
printf("\nPCM states:\n");
for (val = 0; val <= SND_PCM_STATE_LAST; val++)
printf(" %s\n",
snd_pcm_state_name((snd_pcm_state_t)val));
return 0;
}
編譯結果
cust@virtualbox:~/alsa_test$ make
/mcuzone/work/buildroot/output/rockchip_rk3308_release/host/usr/bin/aarch64-rockchip-linux-gnu-gcc -g -Wall -O3 -fpic -I/home/cust/alsa_test -c -o test_one.o test_one.c
/mcuzone/work/buildroot/output/rockchip_rk3308_release/host/usr/bin/aarch64-rockchip-linux-gnu-g++ -g -Wall -O3 -fpic -I/home/cust/alsa_test -c -o main.o main.cpp
/mcuzone/work/buildroot/output/rockchip_rk3308_release/host/usr/bin/aarch64-rockchip-linux-gnu-g++ -g -Wall -O3 -fpic -I/home/cust/alsa_test -o test ./test_one.o ./main.o -L/home/cust/alsa_test -ldl -lpthread -lasound
板子上運行結果
/oem # ./test
ALSA library version: 1.1.5
PCM stream types:
PLAYBACK
CAPTURE
PCM access types:
MMAP_INTERLEAVED
MMAP_NONINTERLEAVED
MMAP_COMPLEX
RW_INTERLEAVED
RW_NONINTERLEAVED
PCM formats:
S8 (Signed 8 bit)
U8 (Unsigned 8 bit)
S16_LE (Signed 16 bit Little Endian)
S16_BE (Signed 16 bit Big Endian)
U16_LE (Unsigned 16 bit Little Endian)
U16_BE (Unsigned 16 bit Big Endian)
S24_LE (Signed 24 bit Little Endian)
S24_BE (Signed 24 bit Big Endian)
U24_LE (Unsigned 24 bit Little Endian)
U24_BE (Unsigned 24 bit Big Endian)
S32_LE (Signed 32 bit Little Endian)
S32_BE (Signed 32 bit Big Endian)
U32_LE (Unsigned 32 bit Little Endian)
U32_BE (Unsigned 32 bit Big Endian)
FLOAT_LE (Float 32 bit Little Endian)
FLOAT_BE (Float 32 bit Big Endian)
FLOAT64_LE (Float 64 bit Little Endian)
FLOAT64_BE (Float 64 bit Big Endian)
IEC958_SUBFRAME_LE (IEC-958 Little Endian)
IEC958_SUBFRAME_BE (IEC-958 Big Endian)
MU_LAW (Mu-Law)
A_LAW (A-Law)
IMA_ADPCM (Ima-ADPCM)
MPEG (MPEG)
GSM (GSM)
SPECIAL (Special)
S24_3LE (Signed 24 bit Little Endian in 3bytes)
S24_3BE (Signed 24 bit Big Endian in 3bytes)
U24_3LE (Unsigned 24 bit Little Endian in 3bytes)
U24_3BE (Unsigned 24 bit Big Endian in 3bytes)
S20_3LE (Signed 20 bit Little Endian in 3bytes)
S20_3BE (Signed 20 bit Big Endian in 3bytes)
U20_3LE (Unsigned 20 bit Little Endian in 3bytes)
U20_3BE (Unsigned 20 bit Big Endian in 3bytes)
S18_3LE (Signed 18 bit Little Endian in 3bytes)
S18_3BE (Signed 18 bit Big Endian in 3bytes)
U18_3LE (Unsigned 18 bit Little Endian in 3bytes)
U18_3BE (Unsigned 18 bit Big Endian in 3bytes)
G723_24 (G.723 (ADPCM) 24 kbit/s, 8 samples in 3 bytes)
G723_24_1B (G.723 (ADPCM) 24 kbit/s, 1 sample in 1 byte)
G723_40 (G.723 (ADPCM) 40 kbit/s, 8 samples in 3 bytes)
G723_40_1B (G.723 (ADPCM) 40 kbit/s, 1 sample in 1 byte)
DSD_U8 (Direct Stream Digital, 1-byte (x8), oldest bit in MSB)
DSD_U16_LE (Direct Stream Digital, 2-byte (x16), little endian, oldest bits in MSB)
DSD_U32_LE (Direct Stream Digital, 4-byte (x32), little endian, oldest bits in MSB)
DSD_U16_BE (Direct Stream Digital, 2-byte (x16), big endian, oldest bits in MSB)
DSD_U32_BE (Direct Stream Digital, 4-byte (x32), big endian, oldest bits in MSB)
PCM subformats:
STD (Standard)
PCM states:
OPEN
SETUP
PREPARED
RUNNING
XRUN
DRAINING
PAUSED
SUSPENDED
DISCONNECTED
以上,混編還是很方便的。