3

/* Edited */

my Cmakefile.txt file is:

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)

In this way, I can compile and link my code. However pthread_create seems not work.

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);

}

Neither "start listening nor "Thread creation" is printed in output. it is as if the function does not return but it doesn't work.

Emanuele Vannacci
  • 321
  • 1
  • 6
  • 15
  • 1
    You don't get a linker error? -> Your program was successfully linked. -> Your issue is very likely not related to CMake but rather your code. -> Show us your code. – Daniel Jour Sep 03 '16 at 22:02
  • Run `make clean; make VERBOSE=1` and show us the linker command it's running at the end. – John Zwinck Sep 03 '16 at 22:51
  • I have modified cmakeList.txt file. Now I can link but pthread_create not work... (I have updated the question) – Emanuele Vannacci Sep 04 '16 at 14:50

1 Answers1

1

CMakeLists.txt is correct. My error was in code. I change pthread_create(data->tid, NULL, job_t, data) != 0 in pthread_create(&data->tid, NULL, job_t, data) != 0.

Emanuele Vannacci
  • 321
  • 1
  • 6
  • 15