Pthreads compile not working - gcc

I have written some code but it doesn't seem to work when I compile it. I am trying to run this in Ubuntu:
#include <pthread.h>
#include <ctype.h>
#include <unistd.h>
char buffer[128];
void *write_thread(void *args)
{
int count = *((int*)args);
write(STDOUT_FILENO, buffer, count);
pthread_exit(0);
}
void *toupper_thread(void *args)
{
int i;
int count = *((int*)args);
for(i = 0; i < count; i++) {
buffer[i] = toupper(buffer[i]);
}
pthread_t writeId;
pthread_create(&writeId, NULL, write_thread, &count);
pthread_join(writeId, NULL);
pthread_exit(0);
}
void *read_thread(void *args)
{
int count = read(STDIN_FILENO, buffer, 128);
pthread_t toupperId;
pthread_create(&toupperId, NULL, toupper_thread, &count);
pthread_join(toupperId, NULL);
//buffer[count] = 0;
pthread_exit(0);
}
int main()
{
pthread_t threadId;
pthread_create(&threadId, NULL, read_thread, NULL);
pthread_join(threadId, NULL);
}
and I get these errors when I try to compile it with gcc -pthread prob41.c or with gcc prob41.c -lpthread:
prob41.c:1:21: error: pthread.h: No such file or directory
prob41.c: In function ‘toupper_thread’:
prob41.c:23: error: ‘pthread_t’ undeclared (first use in this function)
prob41.c:23: error: (Each undeclared identifier is reported only once
prob41.c:23: error: for each function it appears in.)
prob41.c:23: error: expected ‘;’ before ‘writeId’
prob41.c:24: error: ‘writeId’ undeclared (first use in this function)
prob41.c: In function ‘read_thread’:
prob41.c:33: error: ‘pthread_t’ undeclared (first use in this function)
prob41.c:33: error: expected ‘;’ before ‘toupperId’
prob41.c:34: error: ‘toupperId’ undeclared (first use in this function)
prob41.c: In function ‘main’:
prob41.c:45: error: ‘pthread_t’ undeclared (first use in this function)
prob41.c:45: error: expected ‘;’ before ‘threadId’
prob41.c:46: error: ‘threadId’ undeclared (first use in this function)
I don't know what I'm doing wrong.

It seems that you missed some dependencies.
Try this:
$ sudo apt-get install -y libc6-dev

Need to add -I flag in the compile command to enable the compiler to find pthread.h

You don't have a pthread.h library in the system. Try to install the
suitable library for it.
You need to be sure to use #include <sys/type.h>
Don't forget to add the -lpthread while compiling: gcc -lpthread {x}.c -o {y}

Related

Error while building a static Linux binary (with musl-libc) that includes LuaJIT

I've cloned the LuaJIT git repo and built it with:
make STATIC_CC="musl-gcc" BUILDMODE="static"
Then, I compiled a simple Lua "hello world" script into a C header file:
luajit -b test.lua test.h
test.h:
#define luaJIT_BC_test_SIZE 52
static const unsigned char luaJIT_BC_test[] = {
27,76,74,2,10,45,2,0,3,0,2,0,4,54,0,0,0,39,2,1,0,66,0,2,1,75,0,1,0,20,72,101,
108,108,111,32,102,114,111,109,32,76,117,97,33,10,112,114,105,110,116,0
};
After that, I wrote a simple C wrapper by following the official example, test.c:
#include <stdio.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include "test.h"
int main(void) {
int error;
lua_State *L = lua_open();
luaL_openlibs(L);
error = luaL_loadbuffer(L, (const char *) luaJIT_BC_test, luaJIT_BC_test_SIZE, "test") || lua_pcall(L, 0, 0, 0);
if (error) {
fprintf(stderr, "%s", lua_tostring(L, -1));
lua_pop(L, 1);
}
lua_close(L);
return 0;
}
But when I try to build it, it crashes with an error:
$ musl-gcc -static -ILuaJIT/src -LLuaJIT/src -o test test.c -lluajit
/usr/bin/ld: /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/libgcc_eh.a(unwind-dw2-fde-dip.o): in function `_Unwind_Find_FDE':
(.text+0x1953): undefined reference to `_dl_find_object'
collect2: error: ld returned 1 exit status
It's related to libgcc, so I tried building everything with musl-clang, but still got the same error. Can someone explain what I'm missing here?
Figured it out - I needed to build LuaJIT with TARGET_XCFLAGS=-DLUAJIT_NO_UNWIND like so:
make STATIC_CC="musl-gcc" BUILDMODE="static" TARGET_XCFLAGS=-DLUAJIT_NO_UNWIND
I guess this just disables C++ exceptions support, but I'm not sure what the real implications are. Seems to work fine, for now.

go runtime fails to compile

I am just wondering why would go runtime fail to build. How do we pass flags (-fpermissive in this case) to the c compiler which golang compiler is using to build the runtime. I am using gcc-4.6.2 on ubuntu 12.04
../../../thirdparty/go1.4.2/x86_64-unknown-linux-gnu/x86_64-unknown-linux-gnu/src/runtime/cgo/gcc_linux_amd64.c: In function ‘void _cgo_sys_thread_start(ThreadStart*)’:
../../../thirdparty/go1.4.2/x86_64-unknown-linux-gnu/x86_64-unknown-linux-gnu/src/runtime/cgo/gcc_linux_amd64.c:45:41: error: invalid conversion from ‘void*’ to ‘__sigset_t*’ [-fpermissive]
A sample program written also fails to compile, it seems the nil defined in the go code is the problem, i wonder how others are working, when does the golang compiler compiles this runtime code ?
gcc t.c -lpthread -o t
t.c: In function ‘void* hello_world(void*)’:
t.c:12:41: error: invalid conversion from ‘void*’ to ‘__sigset_t*’ [-fpermissive]
/usr/include/x86_64-linux-gnu/bits/sigthread.h:31:12: error: initializing argument 3 of ‘int pthread_sigmask(int, const __sigset_t*, __sigset_t*)’ [-fpermissive]
rk#rk-VirtualBox:~$ gcc -fpermissive t.c -lpthread -o t
t.c: In function ‘void* hello_world(void*)’:
t.c:12:41: warning: invalid conversion from ‘void*’ to ‘__sigset_t*’ [-fpermissive]
rk#rk-VirtualBox:~$ cat t.c
#include<pthread.h>
#include<stdio.h>
#include<signal.h>
#define nil ((void*)0)
static void*
hello_world(void *vptr)
{
sigset_t set;
sigemptyset(&set);
pthread_sigmask(SIG_BLOCK, &set, nil);
printf("hello world");
return NULL;
}
int main(int ac, char **av)
{
pthread_t t;
pthread_create(&t, NULL, hello_world, NULL);
pthread_join(t, NULL);
return 0;
}
/usr/include/x86_64-linux-gnu/bits/sigthread.h:31:12: error: initializing argument 3 of ‘int pthread_sigmask(int, const __sigset_t*, __sigset_t*)’ [-fpermissive]
make: *** [rulemanager] Error 2

ATmega128 - UCSROA undeclared (first use in this function)

I have a question about ATmega128 (UART)
This program sends Hello message through UART.
The following is my code.
#include <avr/io.h>
void putch(unsigned char data)
{
while((UCSROA & 0x20) == 0);
UDR0 = data;
UCSROA |= 0x20;
}
int main()
{
unsigned char text[] = "Hello! World!\r\n";
unsigned char i = 0;
DDRE = 0xFE;
UCSROA = 0x00;
UCSROB = 0x18;
UCSROC = 0x06;
UBRROH = 0x00;
UBRROL = 0x03;
while(text[i] != '\0')
putch(text[i++]);
return 0;
}
This is error messages.
Build succeeded with 0 Warnings...
avr-gcc -mmcu=atmega128 -Wall -gdwarf-2 -std=gnu99 -DF_CPU=7372800UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT Hello.o -MF dep/Hello.o.d -c ../Hello.c
../Hello.c: In function 'putch':
../Hello.c:5: error: 'UCSROA' undeclared (first use in this function)
../Hello.c:5: error: (Each undeclared identifier is reported only once
../Hello.c:5: error: for each function it appears in.)
../Hello.c: In function 'main':
../Hello.c:16: error: 'UCSROA' undeclared (first use in this function)
../Hello.c:17: error: 'UCSROB' undeclared (first use in this function)
../Hello.c:18: error: 'UCSROC' undeclared (first use in this function)
../Hello.c:19: error: 'UBRROH' undeclared (first use in this function)
../Hello.c:20: error: 'UBRROL' undeclared (first use in this function)
make: *** [Hello.o] Error 1 Build failed with 8 errors and 0 warnings...
I tested my code other computers.
But, It's not working.
I don't know how to fix this problem.
Give me some advice.
Thanks.
Its not
UCSROA
with the letter "O", it is
UCSR0A
with the number 0.

clang pass variables by reference

I am compiling the following code using clang 3.4.2 ..
#include <stdio.h>
void haa(int& j){
j=1;
}
int main(){
printf("hello\n");
}
This gives the following error:
hello.c:3:13: error: expected ')'
void haa(int& j){
^
hello.c:3:9: note: to match this '('
void haa(int& j){
^
hello.c:3:13: error: parameter name omitted
void haa(int& j){
^
hello.c:4:2: error: use of undeclared identifier 'j'
j=1;
^
3 errors generated.
Compiling the same with gcc gives no errors or warnings...
Can someone explain why this is happening?
The issue is that pass by reference (with references and not pointers) is not a c but a c++ feature.
You need to compile the code with a c++ compiler such as g++ or clang++. Changing the file extension to .cpp also works, as this tells the compiler to treat it as a c++.

Building with libjson on OSX Mavericks

I've tried the advice on threads here and here to no avail.
I have Xcode 5.0.2 installed and I am compiling everything on the command line. After make/make install to build libjson, I created a simple test file to link and build from it:
#include <iostream>
#include "libjson.h"
int main(int argc, const char * argv[])
{
// insert code here...
std::cout << "Hello, World!\n";
JSONNode n(JSON_NODE);
n.push_back(JSONNode("RootA", "Hello World"));
JSONNode c(JSON_ARRAY);
c.set_name("ArrayOfNumbers");
c.push_back(JSONNode("", 16));
c.push_back(JSONNode("", 42));
c.push_back(JSONNode("", 128));
n.push_back(c);
std::string jc = n.write_formatted();
std::cout << jc << std::endl;
return 0;
}
When I try to build this file:
g++ -DNDEBUG main.cpp -ljson
I get this:
main.cpp:17:5: error: unknown type name 'JSONNode'
JSONNode n(JSON_NODE);
^
main.cpp:18:17: error: use of undeclared identifier 'JSONNode'
n.push_back(JSONNode("RootA", "Hello World"));
^
main.cpp:19:5: error: unknown type name 'JSONNode'
JSONNode c(JSON_ARRAY);
^
main.cpp:21:17: error: use of undeclared identifier 'JSONNode'
c.push_back(JSONNode("", 16));
^
main.cpp:22:17: error: use of undeclared identifier 'JSONNode'
c.push_back(JSONNode("", 42));
^
main.cpp:23:17: error: use of undeclared identifier 'JSONNode'
c.push_back(JSONNode("", 128));
Found the answer from another SO question after I realized the make process had problems. Basically, the solution is to copy the source code into Xcode and build it as part of the project instead of trying to link it as a library.
I also tried to build libjson 7.6.1 on an ubuntu machine (12.04) and encountered the exact problem despite a perfect make.

Resources