This sounds like a problem with linking. I could compile your code without problems using g++:
g++ Test.cpp -L/path/to/my/openCV/lib -lopencv_core -lopencv_imgcodecs -o Test
If you want to use further functions of Open CV you might have to link against additional Open CV libraries.