If you are on linux os, you needn’t add library setting in CMakeLists.txt. The libray iconv files has been inside system path.
cmake_minimum_required(VERSION 3.5)
project(iconvTest LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(iconvTest main.cpp )
target_link_libraries( ${PROJECT_NAME} ) # iconv)
main.cpp
#include <stdio.h>
#include <string.h>
#include <iconv.h>
int main()
{
char *gbk_str = "GBK \xB5\xE7\xCA\xD3\xBB\xFA";
char dest_str[100];
char *out = dest_str;
size_t inbytes = strlen(gbk_str);
size_t outbytes = sizeof( dest_str );
iconv_t conv = iconv_open("UTF-8", "GBK");
if (conv == (iconv_t)-1) {
perror("iconv_open");
return 1;
}
if (iconv(conv, &gbk_str, &inbytes, &out, &outbytes) == (size_t)-1) {
perror("iconv");
return 1;
}
puts(dest_str);
printf( "%d\n", strlen( dest_str ) );
return 0;
}