I've been given some starter code for a project I have to complete in a class I'm taking. The code compiles fine on the university computers however when I try to compile the code on my own computer I get errors due the function call strdup. From what I can gather this is caused because strdup is not a ISO c99 function (https://bugzilla.redhat.com/show_bug.cgi?id=130815). How should I go about getting the code to compile? I'd imagine I just need to throw in some additional compiler flags but I'm not sure which ones. In case you need the info I ran g++ -v, here is the output:
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
The answer of Seth is correct. If it doesn't work for you, there is also the possibility to pass the -std=gnu99 compiler option to g++ (it automatically defines all common test macros).
Be sure to add all options at the end of the command line, because later options overwrite newer ones!
Here are the test macro requirements for GNU glibc (from manpage):
Since glibc 2.12:
_SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
_XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED ||
_POSIX_C_SOURCE >= 200809L
Before glibc 2.12:
_SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
_XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
I don't know why POSIX >= 200809L is required, since the manpage says it conforms to POSIX.1-2001.
Add -D_BSD_SOURCE or -D_SVID_SOURCE to your compile line and you will expose strdup()
Related
Take a simple file:
int main(void) {
return 0;
}
My CMakeLists.txt file:
cmake_minimum_required(VERSION 3.20)
project(TRA
VERSION 0.0.1
DESCRIPTION "STM32 Traffic Controller"
LANGUAGES C ASM)
set(BSP_DRIVER_FILES
BSP/tra/startup_stm32f429xx.s
BSP/tra/Core/Src/system_stm32f4xx.c
)
set(BSP_COMPILER_FLAGS
-DUSE_HAL_DRIVER
-DSTM32F429xx
-I${CMAKE_SOURCE_DIR}/BSP/tra/Core/Inc
-I${CMAKE_SOURCE_DIR}/BSP/tra/Drivers/STM32F4xx_HAL_Driver/Inc
-I${CMAKE_SOURCE_DIR}/BSP/tra/Drivers/STM32F4xx_HAL_Driver/Inc/Legacy
-I${CMAKE_SOURCE_DIR}/BSP/tra/Drivers/CMSIS/Device/ST/STM32F4xx/Include
-I${CMAKE_SOURCE_DIR}/BSP/tra/Drivers/CMSIS/Include
)
add_executable(tra)
target_sources(tra PRIVATE
${BSP_DRIVER_FILES}
code/tra.c
)
set(COMPILER_FLAGS
${BSP_COMPILER_FLAGS}
-O0 -g -ggdb3
-mcpu=cortex-m4 -mthumb -mfloat-abi=soft
-fdata-sections -ffunction-sections
--specs=rdimon.specs
)
target_compile_options(tra PRIVATE
${COMPILER_FLAGS}
)
target_link_options(tra PRIVATE
-mcpu=cortex-m4 -mthumb -mfloat-abi=soft
--specs=rdimon.specs -lm -lc
-Wl,--gc-sections
)
Running qemu-arm build/tra.
I get
qemu: uncaught target signal 11 (Segmentation fault) - core dumped
Segmentation fault (core dumped)
My understanding was that this issue was commonly caused by qemu not handling hardware FPU, however I'm specifying software floating point here. Also, I have removed FPU initialization code from my startup.c file.
With gdb:
# Start
qemu-arm -g 1234 build/tra
# Attach
arm-none-eabi-gdb -q --nh -ex 'file build/tra' -ex 'target remote localhost:1234'
I get the segmentation fault at SystemInit(). Running gdb list, it shows the commented out FPU initialization code, so I don't see how that could be the issue.
Therefore, I'm at a loss as to what is going on.
You're building a bare-metal binary for a Cortex-M CPU, and then trying to run it on qemu-arm, which is the emulator for running Cortex-A Linux binaries.
If you're building a bare-metal binary, you need to build it for the exact machine type that you want to run it on, which means you need to target a machine type supported by qemu-system-arm, and you need to tell qemu-system-arm to use that machine type.
As suggested by Peter Maydell require full system emulation. For cortex-M, this was provided by xPack QEMU
I'm attempting to build redis on my aws linux server in order to get access to the redis-cli and connect to my redis instance which is also running successfully in aws.
my gcc is:
gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
I downloaded the source and started the build:
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make distclean
make
It built the dependencies and then gave me this:
gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/tsd.o src/tsd.c
gcc -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops -c -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -o src/witness.o src/witness.c
ar crus lib/libjemalloc.a src/jemalloc.o src/arena.o src/background_thread.o src/base.o src/bin.o src/bitmap.o src/ckh.o src/ctl.o src/div.o src/extent.o src/extent_dss.o src/extent_mmap.o src/hash.o src/hooks.o src/large.o src/log.o src/malloc_io.o src/mutex.o src/mutex_pool.o src/nstime.o src/pages.o src/prng.o src/prof.o src/rtree.o src/stats.o src/sz.o src/tcache.o src/ticker.o src/tsd.o src/witness.o
make[3]: Leaving directory `/home/ec2-user/redis-stable/deps/jemalloc'
make[2]: Leaving directory `/home/ec2-user/redis-stable/deps'
CC adlist.o
CC quicklist.o
CC ae.o
CC anet.o
CC dict.o
CC server.o
In file included from server.c:30:0:
server.h:1044:5: error: expected specifier-qualifier-list before ‘_Atomic’
_Atomic unsigned int lruclock; /* Clock for LRU eviction */
^
server.c: In function ‘serverLogRaw’:
server.c:1028:31: error: ‘struct redisServer’ has no member named ‘logfile’
int log_to_stdout = server.logfile[0] == '\0';
^
server.c:1031:23: error: ‘struct redisServer’ has no member named ‘verbosity’
if (level < server.verbosity) return;
^
server.c:1033:47: error: ‘struct redisServer’ has no member named ‘logfile’
fp = log_to_stdout ? stdout : fopen(server.logfile,"a");
...and a lot more
I assume that I must have done something wrong as this is code that undoubtedly builds for everyone else. I'm a java developer with little to no c background. Any pointers will be appreciated.
Jay
Appears that gcc >= 4.9 is required to build redis 6x ...
Problem here is latest Redis version(6.0.7) depends on _Atomic which is not supported in older gcc 4.8.5.
One can also compile latest Redis code with clang if updating gcc version is not possible.
$ cd redis-stable
$ CC=clang make
i have successed.
at first i want to install gcc6, but failed again and agin, then i notise that,devtoolset-6 is deprecated. Maybe you should try -7 or -8.
then i use the flow cmd and it worked
yum install -y devtoolset-6-gcc devtoolset-7-gcc-c++
so i hope it can also help you
Thanks for the responses. The solution I ended up with was to build a new server with CentOS8 and gcc 8.3.1. This combination built the redis server 6.0.3 with no issues. Moral: you need to have up to date infra and software in order to properly build new software.
Download older version, seems some issue with 6.0.*
That worked for me on ubunt 14.04 LTS
I compile code with g++ -Wno-nonnull and get
cc1plus: warning: command line option "-Wno-nonnull" is valid for C/ObjC but not for C++
but then anyway
foo.cxx:455:3: error: null argument where non-null required (argument 2)
The compiler is:
> g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib64/gcc/x86_64-suse-linux/4.5/lto-wrapper
Target: x86_64-suse-linux
Configured with: ../configure --prefix=/usr --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64 --enable-languages=c,c++,objc,fortran,obj-c++,java,ada --enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.5 --enable-ssp --disable-libssp --disable-plugin --with-bugurl=http://bugs.opensuse.org/ --with-pkgversion='SUSE Linux' --disable-libgcj --disable-libmudflap --with-slibdir=/lib64 --with-system-zlib --enable-__cxa_atexit --enable-libstdcxx-allocator=new --disable-libstdcxx-pch --enable-version-specific-runtime-libs --program-suffix=-4.5 --enable-linux-futex --without-system-libunwind --enable-gold --with-plugin-ld=/usr/bin/gold --with-arch-32=i586 --with-tune=generic --build=x86_64-suse-linux
Thread model: posix
gcc version 4.5.1 20101208 [gcc-4_5-branch
revision 167585] (SUSE Linux)
For better or worse I do have to support this old compiler. The code compiles fine on g++ 9.2 with -Wno-nonnull.
In man, -Wnonnull is listed among all the (assumably for all languages) "Warning" and not as a "C and Objective-C-only Warning".
Later in the man page, I see -Wnonnull (C and Objective-C only) as the entry for this option specifically.
Any suggestions?
I am getting following error while trying to install httpd 2.0.49 from its source on ubuntu 12.04 machine.
sudo make
...
...
make[4]: Entering directory `/home/satya/httpd-2.0.49/srclib/apr/network_io/unix'
/bin/bash /home/satya/httpd-2.0.49/srclib/apr/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -D_REENTRANT -D_GNU_SOURCE -I../../include -I../../include/arch/unix -I../../include/arch/unix -c sockopt.c && touch sockopt.lo
sockopt.c: In function 'apr_socket_opt_set':
sockopt.c:216:27: error: 'SCTP_NODELAY' undeclared (first use in this function)
I installed lksctp by executing sudo apt-get install lksctp-tools command and made few changes as suggested here
Even after doing this I am unable to compile the httpd and getting same error as above.
This is a bug in the configure script which assumes sctp is available even when it is not. This was fixed in later releases of 2.0.
Preferably, you should - if you want 2.0 - compile 2.0.65 instead.
Do however note that 2.0 is EOL (since 2013), so it is not officially supported by Apache anymore.
I'm following the C++ Cookbook tutorial on static and dynamic library linking with g++. I can build the binary fine, but when I run it I get the error
./hellobeatles: error while loading shared libraries: libjohnpaul.so: cannot open shared object file: No such file or directory
I used the command
: g++ -o hellobeatles hellobeatles.cpp -L ../johnpaul/ -L ../georgeringo/ -ljohnpaul -lgeorgeringo
The program builds and runs fine if I explicitly list the path like
: g++ -o hellobeatles hellobeatles.cpp ../johnpaul/libjohnpaul.so ../georgeringo/libgeorgeringo.so
Am I linking to the libaries incorrectly in the first command? Or is there some configuration setting I need to muck with?
I'm running an Ubuntu 9.10 guest vm in VirtualBox if that matters, and here's the -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.1-4ubuntu9' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu9)
The dynamic linker expects to find shared libraries in /usr/lib, /lib, /usr/local/lib, and possibly a few other places. It will certainly not look for them in ../johnpaul/.
If the libraries are intended to be installed into a global location, then just install them there.
Otherwise, you must tell the dynamic linker where to find them.
A better approach is to add them to RPATH encoded into the executable:
g++ -o hellobeatles hellobeatles.cpp \
-L ../johnpaul/ -L ../georgeringo/ -ljohnpaul -lgeorgeringo \
-Wl,-rpath=/path/to/johnpaul:/path/to/georgeringo
Alternative (and less preferred) approach is to:
export LD_LIBRARY_PATH=/path/to/johnpaul:/path/to/georgeringo