2
/* *編輯/在C使用並行線程的lib通過CMake
我Cmakefile.txt文件是:
cmake_minimum_required(VERSION 3.3)
project(WebServer)
set(CMAKE_C_COMPILER "/usr/bin/gcc")
set(SOURCE_FILES io.c server.c lock_fcntl.c sig_handler.c thread_job.c msg_parser.c)
set(LIB http-parser-master/http_parser.c)
set(CMAKE_USE_PTHREADS_INIT true)
set(CMAKE_USE_PTHREADS_INIT ON)
find_package(Threads REQUIRED)
add_executable(server ${SOURCE_FILES} ${LIB})
target_link_libraries(server Threads::Threads)
add_executable(client client.c io.c)
include_directories(header)
include_directories(http-parser-master)
這樣,我可以編譯和鏈接我的代碼。但是,pthread_create似乎不起作用。
server.c:
void create_thread(int socket) {
data_t *data;
data = malloc(sizeof(data_t));
if (data == NULL) {
fprintf(stderr, "error in memory allocation");
exit(EXIT_FAILURE);
}
data->sock = socket;
/* debug */
fprintf(stdout, "[*] Creating thread\n");
fflush(stdout);
if (pthread_create(data->tid, NULL, job_t, data) != 0) {
fprintf(stderr, "Error returned by pthread_create()\n");
exit(EXIT_FAILURE);
}
/* debug */
fprintf(stdout, "[*] Thread created\n");
fflush(stdout);
}
thread_job.c:
void *job_t(void *arg) {
data_t *data = arg;
char *http_msg_h;
int nread;
/* debug */
fprintf(stdout, "start listening\n");
fflush(stdout);
/* read http header from connsd socket and store it in msg buffer */
nread = receive_msg_h(data->sock, (void **) &http_msg_h);
/* debug */
fprintf(stdout, "[+] parsing completed");
fflush(stdout);
}
既不是「開始聽,也不是 「線程創建」,在輸出打印 這是因爲如果該函數不返回。但它不起作用
您沒有收到鏈接錯誤? - >您的程序已成功鏈接。 - >您的問題很可能與CMake無關,而與您的代碼無關。 - >向我們顯示您的代碼。 –
運行'make clean;使VERBOSE = 1'並向我們展示它在最後運行的鏈接器命令。 –
我修改了cmakeList.txt文件。現在我可以鏈接,但pthread_create不工作...(我已更新問題) –